]> git.pld-linux.org Git - packages/kernel.git/blob - grsecurity-nopax-2.0-rc2-2.4.21.patch
- obsolete
[packages/kernel.git] / grsecurity-nopax-2.0-rc2-2.4.21.patch
1 diff -urN linux-2.4.21/Documentation/Configure.help linux-2.4.21/Documentation/Configure.help
2 --- linux-2.4.21/Documentation/Configure.help   2003-06-23 11:42:34.000000000 -0400
3 +++ linux-2.4.21/Documentation/Configure.help   2003-06-23 11:49:15.000000000 -0400
4 @@ -2609,6 +2609,20 @@
5    If you want to compile it as a module, say M here and read
6    Documentation/modules.txt.  If unsure, say `N'.
7  
8 +stealth networking support
9 +CONFIG_IP_NF_MATCH_STEALTH
10 +  Enabling this option will drop all syn packets coming to unserved tcp
11 +  ports as well as all packets coming to unserved udp ports.  If you
12 +  are using your system to route any type of packets (ie. via NAT)
13 +  you should put this module at the end of your ruleset, since it will 
14 +  drop packets that aren't going to ports that are listening on your 
15 +  machine itself, it doesn't take into account that the packet might be 
16 +  destined for someone on your internal network if you're using NAT for 
17 +  instance.
18 +
19 +  If you want to compile it as a module, say M here and read
20 +  Documentation/modules.txt.  If unsure, say `N'.
21 +
22  MAC address match support
23  CONFIG_IP_NF_MATCH_MAC
24    MAC matching allows you to match packets based on the source
25 @@ -21729,6 +21743,612 @@
26  
27    "Area6" will work for most boards. For ADX, select "Area5".
28  
29 +Grsecurity
30 +CONFIG_GRKERNSEC
31 +  If you say Y here, you will be able to configure many features that
32 +  will enhance the security of your system.  It is highly recommended
33 +  that you say Y here and read through the help for each option so
34 +  you fully understand the features and can evaluate their usefulness
35 +  for your machine.
36 +
37 +Additional security levels
38 +CONFIG_GRKERNSEC_LOW
39 +
40 +  Low additional security
41 +  -----------------------------------------------------------------------
42 +  If you choose this option, several of the grsecurity options will
43 +  be enabled that will give you greater protection against a number
44 +  of attacks, while assuring that none of your software will have any 
45 +  conflicts with the additional security measures.  If you run a lot of 
46 +  unusual software, or you are having problems with the higher security 
47 +  levels, you should say Y here.  With this option, the following features
48 +  are enabled:
49 +  
50 +  linking restrictions
51 +  fifo restrictions
52 +  random pids
53 +  enforcing nproc on execve()
54 +  restricted dmesg
55 +  random ip ids
56 +  enforced chdir("/") on chroot
57 +
58 +  Medium additional security
59 +  -----------------------------------------------------------------------
60 +  If you say Y here, several features in addition to those included in the 
61 +  low additional security level will be enabled.  These features provide
62 +  even more security to your system, though in rare cases they may
63 +  be incompatible with very old or poorly written software.  If you 
64 +  enable this option, make sure that your auth service (identd) is 
65 +  running as gid 10 (usually group wheel). With this option the following 
66 +  features (in addition to those provided in the low additional security 
67 +  level) will be enabled:
68 +
69 +  random tcp source ports
70 +  altered ping ids
71 +  failed fork logging
72 +  time change logging
73 +  signal logging
74 +  deny mounts in chroot
75 +  deny double chrooting
76 +  deny sysctl writes in chroot
77 +  deny mknod in chroot
78 +  deny access to abstract AF_UNIX sockets out of chroot
79 +  deny pivot_root in chroot
80 +  denied writes of /dev/kmem, /dev/mem, and /dev/port
81 +  /proc restrictions with special gid set to 10 (usually wheel)
82 +  address space layout randomization
83 +
84 +  High additional security
85 +  ----------------------------------------------------------------------
86 +  If you say Y here, many of the features of grsecurity will be enabled,
87 +  that will protect you against virtually all kinds of attacks against
88 +  your system.  The much hightened security comes at a cost of an 
89 +  increased chance of incompatabilities with rare software on your 
90 +  machine.  It is highly recommended that you view 
91 +  <http://grsecurity.net/features.htm> and read about each option.
92 +  Also remember that since the /proc restrictions are 
93 +  enabled, you must run your identd as group wheel (gid 10).  
94 +  This security level enables the following features in addition to those
95 +  listed in the low and medium security levels:
96 +
97 +  additional /proc restrictions
98 +  chmod restrictions in chroot
99 +  no signals, ptrace, or viewing processes outside of chroot
100 +  capability restrictions in chroot
101 +  deny fchdir out of chroot
102 +  priority restrictions in chroot
103 +  mprotect restrictions
104 +  removal of /proc/<pid>/[maps|mem]
105 +  kernel stack randomization
106 +  mount/unmount/remount logging
107 +  kernel symbol hiding
108 +
109 +Customized additional security
110 +CONFIG_GRKERNSEC_CUSTOM
111 +  If you say Y here, you will be able to configure every grsecurity 
112 +  option, which allows you to enable many more features that aren't 
113 +  covered in the basic security levels.  These additional features include 
114 +  TPE, socket restrictions, and the sysctl system for grsecurity.  It is 
115 +  advised that you read through the help for each option to determine its 
116 +  usefulness in your situation.
117 +
118 +Deny writing to /dev/kmem, /dev/mem, and /dev/port
119 +CONFIG_GRKERNSEC_KMEM
120 +  If you say Y here, /dev/kmem and /dev/mem won't be allowed to
121 +  be written to via mmap or otherwise to modify the running kernel.
122 +  /dev/port will also not be allowed to be opened. If you have module
123 +  support disabled, enabling this will close up four ways that are
124 +  currently used  to insert malicious code into the running kernel.
125 +  Even with all these features enabled, we still highly recommend that
126 +  you use the ACL system, as it is still possible for an attacker to 
127 +  modify the running kernel through privileged I/O granted by ioperm/iopl.
128 +  If you are not using XFree86, you may be able to stop this additional
129 +  case by enabling the 'Disable privileged I/O' option. Though nothing
130 +  legitimately writes to /dev/kmem, XFree86 does need to write to /dev/mem,
131 +  but only to video memory, which is the only writing we allow in this
132 +  case.  If /dev/kmem or /dev/mem are mmaped without PROT_WRITE, they will
133 +  not be allowed to mprotect it with PROT_WRITE later.
134 +  Enabling this feature could make certain apps like VMWare stop working,
135 +  as they need to write to other locations in /dev/mem.
136 +  There are a few video cards that require write access to the BIOS,
137 +  one of which is the Savage.  If you have this video card, you must say 
138 +  N here, or Xfree86 will not function.
139 +  It is highly recommended that you say Y here if you meet all the 
140 +  conditions above.
141 +
142 +Disable privileged I/O
143 +CONFIG_GRKERNSEC_IO
144 +  If you say Y here, all ioperm and iopl calls will return an error.
145 +  Ioperm and iopl can be used to modify the running kernel.
146 +  Unfortunately, some problems need this access to operate properly,
147 +  the most notable of which are XFree86 and hwclock.  hwclock can be
148 +  remedied by having RTC support in the kernel, so CONFIG_RTC is
149 +  enabled if this option is enabled, to ensure that hwclock operates
150 +  correctly.  XFree86 still will not operate correctly with this option
151 +  enabled, so DO NOT CHOOSE Y IF YOU USE XFree86.  If you use XFree86
152 +  and you still want to protect your kernel against modification,
153 +  use the ACL system.
154 +
155 +Hide kernel symbols
156 +CONFIG_GRKERNSEC_HIDESYM
157 +  If you say Y here, getting information on loaded modules, and 
158 +  displaying all kernel symbols through a syscall will be restricted
159 +  to users with CAP_SYS_MODULE.  This option is only effective 
160 +  provided the following conditions are met:
161 +  1) The kernel using grsecurity is not precompiled by some distribution
162 +  2) You are using the ACL system and hiding other files such as your
163 +     kernel image and System.map
164 +  3) You have the additional /proc restrictions enabled, which removes
165 +     /proc/kcore
166 +  If the above conditions are met, this option will aid to provide a
167 +  useful protection against local and remote kernel exploitation of
168 +  overflows and arbitrary read/write vulnerabilities.
169 +
170 +/proc/<pid>/ipaddr support
171 +CONFIG_GRKERNSEC_PROC_IPADDR
172 +  If you say Y here, a new entry will be added to each /proc/<pid>
173 +  directory that contains the IP address of the person using the task.
174 +  The IP is carried across local TCP and AF_UNIX stream sockets.
175 +  This information can be useful for IDS/IPSes to perform remote response
176 +  to a local attack.  The entry is readable by only the owner of the 
177 +  process (and root if he has CAP_DAC_OVERRIDE, which can be removed via
178 +  the RBAC system), and thus does not create privacy concerns.
179 +
180 +Proc Restrictions
181 +CONFIG_GRKERNSEC_PROC
182 +  If you say Y here, the permissions of the /proc filesystem
183 +  will be altered to enhance system security and privacy.  Depending
184 +  upon the options you choose, you can either restrict users to see
185 +  only the processes they themselves run, or choose a group that can
186 +  view all processes and files normally restricted to root if you choose
187 +  the "restrict to user only" option.  NOTE: If you're running identd as 
188 +  a non-root user, you will have to run it as the group you specify here.
189 +
190 +Restrict /proc to user only
191 +CONFIG_GRKERNSEC_PROC_USER
192 +  If you say Y here, non-root users will only be able to view their own 
193 +  processes, and restricts them from viewing network-related information,  
194 +  and viewing kernel symbol and module information.
195 +
196 +Restrict /proc to user and group
197 +CONFIG_GRKERNSEC_PROC_USERGROUP
198 +  If you say Y here, you will be able to select a group that will be
199 +  able to view all processes, network-related information, and
200 +  kernel and symbol information.  This option is useful if you want
201 +  to run identd as a non-root user.
202 +
203 +Additional proc restrictions
204 +CONFIG_GRKERNSEC_PROC_ADD
205 +  If you say Y here, additional restrictions will be placed on
206 +  /proc that keep normal users from viewing cpu and device information.
207 +
208 +Dmesg(8) Restriction
209 +CONFIG_GRKERNSEC_DMESG
210 +  If you say Y here, non-root users will not be able to use dmesg(8)
211 +  to view up to the last 4kb of messages in the kernel's log buffer.
212 +  If the sysctl option is enabled, a sysctl option with name "dmesg" is 
213 +  created.
214 +
215 +Linking restrictions
216 +CONFIG_GRKERNSEC_LINK
217 +  If you say Y here, /tmp race exploits will be prevented, since users
218 +  will no longer be able to follow symlinks owned by other users in 
219 +  world-writeable +t directories (i.e. /tmp), unless the owner of the 
220 +  symlink is the owner of the directory. users will also not be
221 +  able to hardlink to files they do not own.  If the sysctl option is
222 +  enabled, a sysctl option with name "linking_restrictions" is created.
223 +
224 +FIFO restrictions
225 +CONFIG_GRKERNSEC_FIFO
226 +  If you say Y here, users will not be able to write to FIFOs they don't
227 +  own in world-writeable +t directories (i.e. /tmp), unless the owner of
228 +  the FIFO is the same owner of the directory it's held in.  If the sysctl
229 +  option is enabled, a sysctl option with name "fifo_restrictions" is 
230 +  created.
231 +
232 +Enforce RLIMIT_NPROC on execs
233 +CONFIG_GRKERNSEC_EXECVE
234 +  If you say Y here, users with a resource limit on processes will
235 +  have the value checked during execve() calls.  The current system
236 +  only checks the system limit during fork() calls.  If the sysctl option
237 +  is enabled, a sysctl option with name "execve_limiting" is created.
238 +
239 +Single group for auditing
240 +CONFIG_GRKERNSEC_AUDIT_GROUP
241 +  If you say Y here, the exec, chdir, (un)mount, and ipc logging features
242 +  will only operate on a group you specify.  This option is recommended
243 +  if you only want to watch certain users instead of having a large
244 +  amount of logs from the entire system.  If the sysctl option is enabled,
245 +  a sysctl option with name "audit_group" is created.
246 +
247 +GID for auditing
248 +CONFIG_GRKERNSEC_AUDIT_GID
249 +  Here you can choose the GID that will be the target of kernel auditing.
250 +  Remember to add the users you want to log to the GID specified here.
251 +  If the sysctl option is enabled, whatever you choose here won't matter. 
252 +  You'll have to specify the GID in your bootup script by echoing the GID 
253 +  to the proper /proc entry.  View the help on the sysctl option for more 
254 +  information.  If the sysctl option is enabled, a sysctl option with name 
255 +  "audit_gid" is created.
256 +
257 +Chdir logging
258 +CONFIG_GRKERNSEC_AUDIT_CHDIR
259 +  If you say Y here, all chdir() calls will be logged.  If the sysctl 
260 +  option is enabled, a sysctl option with name "audit_chdir" is created.
261 +
262 +(Un)Mount logging
263 +CONFIG_GRKERNSEC_AUDIT_MOUNT
264 +  If you say Y here, all mounts and unmounts will be logged.  If the 
265 +  sysctl option is enabled, a sysctl option with name "audit_mount" is 
266 +  created.
267 +
268 +IPC logging
269 +CONFIG_GRKERNSEC_AUDIT_IPC
270 +  If you say Y here, creation and removal of message queues, semaphores,
271 +  and shared memory will be logged.  If the sysctl option is enabled, a
272 +  sysctl option with name "audit_ipc" is created.
273 +
274 +Exec logging
275 +CONFIG_GRKERNSEC_EXECLOG
276 +  If you say Y here, all execve() calls will be logged (since the
277 +  other exec*() calls are frontends to execve(), all execution
278 +  will be logged).  Useful for shell-servers that like to keep track
279 +  of their users.  If the sysctl option is enabled, a sysctl option with
280 +  name "exec_logging" is created.
281 +  WARNING: This option when enabled will produce a LOT of logs, especially
282 +  on an active system.
283 +
284 +Resource logging
285 +CONFIG_GRKERNSEC_RESLOG
286 +  If you say Y here, all attempts to overstep resource limits will
287 +  be logged with the resource name, the requested size, and the current
288 +  limit.  It is highly recommended that you say Y here.
289 +
290 +Signal logging
291 +CONFIG_GRKERNSEC_SIGNAL
292 +  If you say Y here, certain important signals will be logged, such as
293 +  SIGSEGV, which will as a result inform you of when a error in a program
294 +  occurred, which in some cases could mean a possible exploit attempt.
295 +  If the sysctl option is enabled, a sysctl option with name 
296 +  "signal_logging" is created.
297 +
298 +Fork failure logging
299 +CONFIG_GRKERNSEC_FORKFAIL
300 +  If you say Y here, all failed fork() attempts will be logged.
301 +  This could suggest a fork bomb, or someone attempting to overstep
302 +  their process limit.  If the sysctl option is enabled, a sysctl option
303 +  with name "forkfail_logging" is created.
304 +
305 +Time change logging
306 +CONFIG_GRKERNSEC_TIME
307 +  If you say Y here, any changes of the system clock will be logged.
308 +  If the sysctl option is enabled, a sysctl option with name 
309 +  "timechange_logging" is created.
310 +
311 +Chroot jail restrictions
312 +CONFIG_GRKERNSEC_CHROOT
313 +  If you say Y here, you will be able to choose several options that will
314 +  make breaking out of a chrooted jail much more difficult.  If you
315 +  encounter no software incompatibilities with the following options, it
316 +  is recommended that you enable each one.
317 +
318 +Deny access to abstract AF_UNIX sockets out of chroot
319 +CONFIG_GRKERNSEC_CHROOT_UNIX
320 +  If you say Y here, processes inside a chroot will not be able to
321 +  connect to abstract (meaning not belonging to a filesystem) Unix
322 +  domain sockets that were bound outside of a chroot.  It is recommended
323 +  that you say Y here.  If the sysctl option is enabled, a sysctl option
324 +  with name "chroot_deny_unix" is created.
325 +
326 +Deny shmat() out of chroot
327 +CONFIG_GRKERNSEC_CHROOT_SHMAT
328 +  If you say Y here, processes inside a chroot will not be able to attach
329 +  to shared memory segments that were created outside of the chroot jail.
330 +  It is recommended that you say Y here.  If the sysctl option is enabled,
331 +  a sysctl option with name "chroot_deny_shmat" is created.
332 +
333 +Protect outside processes
334 +CONFIG_GRKERNSEC_CHROOT_FINDTASK
335 +  If you say Y here, processes inside a chroot will not be able to
336 +  kill, send signals with fcntl, ptrace, capget, setpgid, getpgid, 
337 +  getsid, or view any process outside of the chroot.  If the sysctl 
338 +  option is enabled, a sysctl option with name "chroot_findtask" is 
339 +  created.
340 +
341 +Deny mounts in chroot
342 +CONFIG_GRKERNSEC_CHROOT_MOUNT
343 +  If you say Y here, processes inside a chroot will not be able to
344 +  mount or remount filesystems.  If the sysctl option is enabled, a 
345 +  sysctl option with name "chroot_deny_mount" is created.
346 +
347 +Deny pivot_root in chroot
348 +CONFIG_GRKERNSEC_CHROOT_PIVOT
349 +  If you say Y here, processes inside a chroot will not be able to use
350 +  a function called pivot_root() that was introduced in Linux 2.3.41.  It 
351 +  works similar to chroot in that it changes the root filesystem.  This 
352 +  function could be misused in a chrooted process to attempt to break out 
353 +  of the chroot, and therefore should not be allowed.  If the sysctl 
354 +  option is enabled, a sysctl option with name "chroot_deny_pivot" is 
355 +  created.
356 +
357 +Deny double-chroots
358 +CONFIG_GRKERNSEC_CHROOT_DOUBLE
359 +  If you say Y here, processes inside a chroot will not be able to chroot
360 +  again.  This is a widely used method of breaking out of a chroot jail
361 +  and should not be allowed.  If the sysctl option is enabled, a sysctl
362 +  option with name "chroot_deny_chroot" is created.
363 +
364 +Deny fchdir outside of chroot
365 +CONFIG_GRKERNSEC_CHROOT_FCHDIR
366 +  If you say Y here, a well-known method of breaking chroots by fchdir'ing
367 +  to a file descriptor of the chrooting process that points to a directory
368 +  outside the filesystem will be stopped.  If the sysctl option
369 +  is enabled, a sysctl option with name "chroot_deny_fchdir" is created.
370 +
371 +Enforce chdir("/") on all chroots
372 +CONFIG_GRKERNSEC_CHROOT_CHDIR
373 +  If you say Y here, the current working directory of all newly-chrooted
374 +  applications will be set to the the root directory of the chroot.
375 +  The man page on chroot(2) states:
376 +  Note that this call does not change  the  current  working
377 +  directory,  so  that `.' can be outside the tree rooted at
378 +  `/'.  In particular, the  super-user  can  escape  from  a
379 +  `chroot jail' by doing `mkdir foo; chroot foo; cd ..'.  
380 +
381 +  It is recommended that you say Y here, since it's not known to break
382 +  any software.  If the sysctl option is enabled, a sysctl option with
383 +  name "chroot_enforce_chdir" is created.
384 +
385 +Deny (f)chmod +s in chroot
386 +CONFIG_GRKERNSEC_CHROOT_CHMOD
387 +  If you say Y here, processes inside a chroot will not be able to chmod
388 +  or fchmod files to make them have suid or sgid bits.  This protects 
389 +  against another published method of breaking a chroot.  If the sysctl 
390 +  option is enabled, a sysctl option with name "chroot_deny_chmod" is
391 +  created.
392 +
393 +Deny mknod in chroot
394 +CONFIG_GRKERNSEC_CHROOT_MKNOD
395 +  If you say Y here, processes inside a chroot will not be allowed to
396 +  mknod.  The problem with using mknod inside a chroot is that it
397 +  would allow an attacker to create a device entry that is the same
398 +  as one on the physical root of your system, which could range from
399 +  anyhing from the console device to a device for your harddrive (which
400 +  they could then use to wipe the drive or steal data).  It is recommended
401 +  that you say Y here, unless you run into software incompatibilities.
402 +  If the sysctl option is enabled, a sysctl option with name
403 +  "chroot_deny_mknod" is created.
404 +
405 +Restrict priority changes in chroot
406 +CONFIG_GRKERNSEC_CHROOT_NICE
407 +  If you say Y here, processes inside a chroot will not be able to raise
408 +  the priority of processes in the chroot, or alter the priority of 
409 +  processes outside the chroot.  This provides more security than simply
410 +  removing CAP_SYS_NICE from the process' capability set.  If the
411 +  sysctl option is enabled, a sysctl option with name "chroot_restrict_nice"
412 +  is created.
413 +
414 +Log all execs within chroot
415 +CONFIG_GRKERNSEC_CHROOT_EXECLOG
416 +  If you say Y here, all executions inside a chroot jail will be logged 
417 +  to syslog.  This can cause a large amount of logs if certain
418 +  applications (eg. djb's daemontools) are installed on the system, and
419 +  is therefore left as an option.  If the sysctl option is enabled, a 
420 +  sysctl option with name "chroot_execlog" is created.
421 +
422 +Deny sysctl writes in chroot
423 +CONFIG_GRKERNSEC_CHROOT_SYSCTL
424 +  If you say Y here, an attacker in a chroot will not be able to
425 +  write to sysctl entries, either by sysctl(2) or through a /proc
426 +  interface.  It is strongly recommended that you say Y here. If the
427 +  sysctl option is enabled, a sysctl option with name 
428 +  "chroot_deny_sysctl" is created.
429 +
430 +Chroot jail capability restrictions
431 +CONFIG_GRKERNSEC_CHROOT_CAPS
432 +  If you say Y here, the capabilities on all root processes within a
433 +  chroot jail will be lowered to stop module insertion, raw i/o,
434 +  system and net admin tasks, rebooting the system, modifying immutable 
435 +  files, modifying IPC owned by another, and changing the system time.
436 +  This is left an option because it can break some apps.  Disable this
437 +  if your chrooted apps are having problems performing those kinds of
438 +  tasks.  If the sysctl option is enabled, a sysctl option with
439 +  name "chroot_caps" is created.
440 +
441 +Trusted path execution
442 +CONFIG_GRKERNSEC_TPE
443 +  If you say Y here, you will be able to choose a gid to add to the
444 +  supplementary groups of users you want to mark as "untrusted."
445 +  These users will not be able to execute any files that are not in
446 +  root-owned directories writeable only by root.  If the sysctl option
447 +  is enabled, a sysctl option with name "tpe" is created.
448 +
449 +Group for trusted path execution
450 +CONFIG_GRKERNSEC_TPE_GID
451 +  Here you can choose the GID to enable trusted path protection for.
452 +  Remember to add the users you want protection enabled for to the GID 
453 +  specified here.  If the sysctl option is enabled, whatever you choose
454 +  here won't matter. You'll have to specify the GID in your bootup 
455 +  script by echoing the GID to the proper /proc entry.  View the help
456 +  on the sysctl option for more information.  If the sysctl option is
457 +  enabled, a sysctl option with name "tpe_gid" is created.
458 +
459 +Partially restrict non-root users
460 +CONFIG_GRKERNSEC_TPE_ALL
461 +  If you say Y here, All non-root users other than the ones in the 
462 +  group specified in the main TPE option will only be allowed to
463 +  execute files in directories they own that are not group or
464 +  world-writeable, or in directories owned by root and writeable only by
465 +  root.  If the sysctl option is enabled, a sysctl option with name 
466 +  "tpe_restrict_all" is created.
467 +
468 +Randomized PIDs
469 +CONFIG_GRKERNSEC_RANDPID
470 +  If you say Y here, all PIDs created on the system will be
471 +  pseudo-randomly generated.  This is extremely effective along
472 +  with the /proc restrictions to disallow an attacker from guessing
473 +  pids of daemons, etc.  PIDs are also used in some cases as part
474 +  of a naming system for temporary files, so this option would keep
475 +  those filenames from being predicted as well.  We also use code
476 +  to make sure that PID numbers aren't reused too soon.  If the sysctl
477 +  option is enabled, a sysctl option with name "rand_pids" is created.
478 +
479 +Larger entropy pools
480 +CONFIG_GRKERNSEC_RANDNET
481 +  If you say Y here, the entropy pools used for many features of Linux
482 +  and grsecurity will be doubled in size.  Since several grsecurity 
483 +  features use additional randomness, it is recommended that you say Y 
484 +  here.  Saying Y here has a similar effect as modifying
485 +  /proc/sys/kernel/random/poolsize.
486 +
487 +Truly random TCP ISN selection
488 +CONFIG_GRKERNSEC_RANDISN
489 +  If you say Y here, Linux's default selection of TCP Initial Sequence
490 +  Numbers (ISNs) will be replaced with that of OpenBSD.  Linux uses
491 +  an MD4 hash based on the connection plus a time value to create the
492 +  ISN, while OpenBSD's selection is random.  If the sysctl option is 
493 +  enabled, a sysctl option with name "rand_isns" is created.
494 +
495 +Randomized IP IDs
496 +CONFIG_GRKERNSEC_RANDID
497 +  If you say Y here, all the id field on all outgoing packets
498 +  will be randomized.  This hinders os fingerprinters and
499 +  keeps your machine from being used as a bounce for an untraceable
500 +  portscan.  Ids are used for fragmented packets, fragments belonging
501 +  to the same packet have the same id.  By default linux only
502 +  increments the id value on each packet sent to an individual host.
503 +  We use a port of the OpenBSD random ip id code to achieve the
504 +  randomness, while keeping the possibility of id duplicates to
505 +  near none.  If the sysctl option is enabled, a sysctl option with name
506 +  "rand_ip_ids" is created.
507 +
508 +Randomized TCP source ports
509 +CONFIG_GRKERNSEC_RANDSRC
510 +  If you say Y here, situations where a source port is generated on the
511 +  fly for the TCP protocol (ie. with connect() ) will be altered so that
512 +  the source port is generated at random, instead of a simple incrementing
513 +  algorithm.  If the sysctl option is enabled, a sysctl option with name
514 +  "rand_tcp_src_ports" is created.
515 +
516 +Randomized RPC XIDs
517 +CONFIG_GRKERNSEC_RANDRPC
518 +  If you say Y here, the method of determining XIDs for RPC requests will
519 +  be randomized, instead of using linux's default behavior of simply
520 +  incrementing the XID.  If you want your RPC connections to be more
521 +  secure, say Y here.  If the sysctl option is enabled, a sysctl option 
522 +  with name "rand_rpc" is created.
523 +
524 +Socket restrictions
525 +CONFIG_GRKERNSEC_SOCKET
526 +  If you say Y here, you will be able to choose from several options.
527 +  If you assign a GID on your system and add it to the supplementary
528 +  groups of users you want to restrict socket access to, this patch
529 +  will perform up to three things, based on the option(s) you choose.
530 +
531 +Deny all socket access
532 +CONFIG_GRKERNSEC_SOCKET_ALL
533 +  If you say Y here, you will be able to choose a GID of whose users will
534 +  be unable to connect to other hosts from your machine or run server
535 +  applications from your machine.  If the sysctl option is enabled, a
536 +  sysctl option with name "socket_all" is created.
537 +
538 +Group for disabled socket access
539 +CONFIG_GRKERNSEC_SOCKET_ALL_GID
540 +  Here you can choose the GID to disable socket access for. Remember to 
541 +  add the users you want socket access disabled for to the GID 
542 +  specified here.  If the sysctl option is enabled, whatever you choose
543 +  here won't matter. You'll have to specify the GID in your bootup 
544 +  script by echoing the GID to the proper /proc entry.  View the help
545 +  on the sysctl option for more information.  If the sysctl option is
546 +  enabled, a sysctl option with name "socket_all_gid" is created.
547 +
548 +Deny all client socket access
549 +CONFIG_GRKERNSEC_SOCKET_CLIENT
550 +  If you say Y here, you will be able to choose a GID of whose users will
551 +  be unable to connect to other hosts from your machine, but will be
552 +  able to run servers.  If this option is enabled, all users in the group
553 +  you specify will have to use passive mode when initiating ftp transfers
554 +  from the shell on your machine.  If the sysctl option is enabled, a
555 +  sysctl option with name "socket_client" is created.
556 +
557 +Group for disabled client socket access
558 +CONFIG_GRKERNSEC_SOCKET_CLIENT_GID
559 +  Here you can choose the GID to disable client socket access for. 
560 +  Remember to add the users you want client socket access disabled for to 
561 +  the GID specified here.  If the sysctl option is enabled, whatever you 
562 +  choose here won't matter. You'll have to specify the GID in your bootup 
563 +  script by echoing the GID to the proper /proc entry.  View the help
564 +  on the sysctl option for more information.  If the sysctl option is
565 +  enabled, a sysctl option with name "socket_client_gid" is created.
566 +
567 +Deny all server socket access
568 +CONFIG_GRKERNSEC_SOCKET_SERVER
569 +  If you say Y here, you will be able to choose a GID of whose users will
570 +  be unable to run server applications from your machine.  If the sysctl 
571 +  option is enabled, a sysctl option with name "socket_server" is created.
572 +
573 +Group for disabled server socket access
574 +CONFIG_GRKERNSEC_SOCKET_SERVER_GID
575 +  Here you can choose the GID to disable server socket access for. 
576 +  Remember to add the users you want server socket access disabled for to 
577 +  the GID specified here.  If the sysctl option is enabled, whatever you 
578 +  choose here won't matter. You'll have to specify the GID in your bootup 
579 +  script by echoing the GID to the proper /proc entry.  View the help
580 +  on the sysctl option for more information.  If the sysctl option is
581 +  enabled, a sysctl option with name "socket_server_gid" is created.
582 +
583 +Sysctl support
584 +CONFIG_GRKERNSEC_SYSCTL
585 +  If you say Y here, you will be able to change the options that
586 +  grsecurity runs with at bootup, without having to recompile your
587 +  kernel.  You can echo values to files in /proc/sys/kernel/grsecurity
588 +  to enable (1) or disable (0) various features.  All the sysctl entries
589 +  are mutable until the "grsec_lock" entry is set to a non-zero value.
590 +  All features are disabled by default. Please note that this option could 
591 +  reduce the effectiveness of the added security of this patch if an ACL 
592 +  system is not put in place.  Your init scripts should be read-only, and 
593 +  root should not have access to adding modules or performing raw i/o 
594 +  operations.  All options should be set at startup, and the grsec_lock 
595 +  entry should be set to a non-zero value after all the options are set.  
596 +  *THIS IS EXTREMELY IMPORTANT*
597 +
598 +Number of burst messages
599 +CONFIG_GRKERNSEC_FLOODBURST
600 +  This option allows you to choose the maximum number of messages allowed
601 +  within the flood time interval you chose in a separate option.  The 
602 +  default should be suitable for most people, however if you find that 
603 +  many of your logs are being interpreted as flooding, you may want to 
604 +  raise this value.
605 +
606 +Seconds in between log messages
607 +CONFIG_GRKERNSEC_FLOODTIME
608 +  This option allows you to enforce the number of seconds between
609 +  grsecurity log messages.  The default should be suitable for most 
610 +  people, however, if you choose to change it, choose a value small enough
611 +  to allow informative logs to be produced, but large enough to
612 +  prevent flooding.
613 +
614 +Hide kernel processes
615 +CONFIG_GRKERNSEC_ACL_HIDEKERN
616 +  If you say Y here, when the ACL system is enabled via gradm -E,
617 +  an additional ACL will be passed to the kernel that hides all kernel
618 +  processes.  These processes will only be viewable by the authenticated
619 +  admin, or processes that have viewing access set.
620 +
621 +Maximum tries before password lockout
622 +CONFIG_GRKERNSEC_ACL_MAXTRIES
623 +  This option enforces the maximum number of times a user can attempt
624 +  to authorize themselves with the grsecurity ACL system before being
625 +  denied the ability to attempt authorization again for a specified time.  
626 +  The lower the number, the harder it will be to brute-force a password.
627 +
628 +Time to wait after max password tries, in seconds
629 +CONFIG_GRKERNSEC_ACL_TIMEOUT
630 +  This option specifies the time the user must wait after attempting to 
631 +  authorize to the ACL system with the maximum number of invalid 
632 +  passwords.  The higher the number, the harder it will be to brute-force 
633 +  a password.
634 +
635  Disable data cache
636  CONFIG_DCACHE_DISABLE
637    This option allows you to run the kernel with data cache disabled.
638 diff -urN linux-2.4.21/Makefile linux-2.4.21/Makefile
639 --- linux-2.4.21/Makefile       2003-06-23 11:41:28.000000000 -0400
640 +++ linux-2.4.21/Makefile       2003-06-23 11:49:15.000000000 -0400
641 @@ -123,9 +123,10 @@
642  CORE_FILES     =kernel/kernel.o mm/mm.o fs/fs.o ipc/ipc.o
643  NETWORKS       =net/network.o
644  CRYPTO         =crypto/crypto.o
645 +GRSECURITY     =grsecurity/grsec.o
646  
647  LIBS           =$(TOPDIR)/lib/lib.a
648 -SUBDIRS                =kernel drivers mm fs net ipc lib crypto
649 +SUBDIRS                =kernel drivers mm fs net ipc lib crypto grsecurity
650  
651  DRIVERS-n :=
652  DRIVERS-y :=
653 @@ -265,7 +266,7 @@
654  
655  export CPPFLAGS CFLAGS CFLAGS_KERNEL AFLAGS AFLAGS_KERNEL
656  
657 -export NETWORKS CRYPTO DRIVERS LIBS HEAD LDFLAGS LINKFLAGS MAKEBOOT ASFLAGS
658 +export NETWORKS CRYPTO DRIVERS LIBS HEAD LDFLAGS LINKFLAGS MAKEBOOT ASFLAGS GRSECURITY
659  
660  .S.s:
661         $(CPP) $(AFLAGS) $(AFLAGS_KERNEL) -traditional -o $*.s $<
662 @@ -284,6 +285,7 @@
663                 $(CORE_FILES) \
664                 $(DRIVERS) \
665                 $(NETWORKS) \
666                 $(CRYPTO) \
667 +               $(GRSECURITY) \
668                 $(LIBS) \
669                 --end-group \
670                 -o vmlinux
671 diff -urN linux-2.4.21/arch/alpha/config.in linux-2.4.21/arch/alpha/config.in
672 --- linux-2.4.21/arch/alpha/config.in   2003-06-23 11:42:24.000000000 -0400
673 +++ linux-2.4.21/arch/alpha/config.in   2003-06-23 11:49:15.000000000 -0400
674 @@ -444,3 +444,12 @@
675  endmenu
676  
677  source lib/Config.in
678 +
679 +mainmenu_option next_comment
680 +comment 'Grsecurity'
681 +bool 'Grsecurity' CONFIG_GRKERNSEC
682 +if [ "$CONFIG_GRKERNSEC" = "y" ]; then
683 +       source grsecurity/Config.in
684 +fi
685 +endmenu
686 +
687 diff -urN linux-2.4.21/arch/alpha/kernel/osf_sys.c linux-2.4.21/arch/alpha/kernel/osf_sys.c
688 --- linux-2.4.21/arch/alpha/kernel/osf_sys.c    2003-06-23 11:42:24.000000000 -0400
689 +++ linux-2.4.21/arch/alpha/kernel/osf_sys.c    2003-06-23 11:49:15.000000000 -0400
690 @@ -33,6 +33,7 @@
691  #include <linux/file.h>
692  #include <linux/types.h>
693  #include <linux/ipc.h>
694 +#include <linux/grsecurity.h>
695  
696  #include <asm/fpu.h>
697  #include <asm/io.h>
698 @@ -240,6 +246,13 @@
699                 if (!file)
700                         goto out;
701         }
702 +
703 +       if(gr_handle_mmap(file, prot)) {
704 +               fput(file);
705 +               ret = -EACCES;
706 +               goto out;
707 +       }
708 +
709         flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
710         down_write(&current->mm->mmap_sem);
711         ret = do_mmap(file, addr, len, prot, flags, off);
712 diff -urN linux-2.4.21/arch/alpha/kernel/ptrace.c linux-2.4.21/arch/alpha/kernel/ptrace.c
713 --- linux-2.4.21/arch/alpha/kernel/ptrace.c     2003-06-23 11:42:24.000000000 -0400
714 +++ linux-2.4.21/arch/alpha/kernel/ptrace.c     2003-06-23 11:49:15.000000000 -0400
715 @@ -13,6 +13,7 @@
716  #include <linux/ptrace.h>
717  #include <linux/user.h>
718  #include <linux/slab.h>
719 +#include <linux/grsecurity.h>
720  
721  #include <asm/uaccess.h>
722  #include <asm/pgtable.h>
723 @@ -275,6 +276,10 @@
724         read_unlock(&tasklist_lock);
725         if (!child)
726                 goto out_notsk;
727 +
728 +       if(gr_handle_ptrace(child, request))
729 +               goto out;
730 +
731         if (request == PTRACE_ATTACH) {
732                 ret = ptrace_attach(child);
733                 goto out;
734 diff -urN linux-2.4.21/arch/arm/config.in linux-2.4.21/arch/arm/config.in
735 --- linux-2.4.21/arch/arm/config.in     2003-06-23 11:42:30.000000000 -0400
736 +++ linux-2.4.21/arch/arm/config.in     2003-06-23 11:49:15.000000000 -0400
737 @@ -657,3 +657,11 @@
738  endmenu
739  
740  source lib/Config.in
741 +
742 +mainmenu_option next_comment
743 +comment 'Grsecurity'
744 +bool 'Grsecurity' CONFIG_GRKERNSEC
745 +if [ "$CONFIG_GRKERNSEC" = "y" ]; then
746 +       source grsecurity/Config.in
747 +fi
748 +endmenu
749 diff -urN linux-2.4.21/arch/cris/config.in linux-2.4.21/arch/cris/config.in
750 --- linux-2.4.21/arch/cris/config.in    2003-06-23 11:42:34.000000000 -0400
751 +++ linux-2.4.21/arch/cris/config.in    2003-06-23 11:49:15.000000000 -0400
752 @@ -263,3 +263,12 @@
753  
754  source lib/Config.in
755  endmenu
756 +
757 +mainmenu_option next_comment
758 +comment 'Grsecurity'
759 +bool 'Grsecurity' CONFIG_GRKERNSEC
760 +if [ "$CONFIG_GRKERNSEC" = "y" ]; then
761 +    source grsecurity/Config.in
762 +fi
763 +endmenu
764 +
765 diff -urN linux-2.4.21/arch/i386/config.in linux-2.4.21/arch/i386/config.in
766 --- linux-2.4.21/arch/i386/config.in    2003-06-23 11:42:23.000000000 -0400
767 +++ linux-2.4.21/arch/i386/config.in    2003-06-23 11:49:15.000000000 -0400
768 @@ -485,3 +485,11 @@
769  endmenu
770  
771  source lib/Config.in
772 +
773 +mainmenu_option next_comment
774 +comment 'Grsecurity'
775 +bool 'Grsecurity' CONFIG_GRKERNSEC
776 +if [ "$CONFIG_GRKERNSEC" = "y" ]; then
777 +       source grsecurity/Config.in
778 +fi
779 +endmenu
780 diff -urN linux-2.4.21/arch/i386/kernel/i387.c linux-2.4.21/arch/i386/kernel/i387.c
781 --- linux-2.4.21/arch/i386/kernel/i387.c        2003-06-23 11:42:24.000000000 -0400
782 +++ linux-2.4.21/arch/i386/kernel/i387.c        2003-06-23 11:49:15.000000000 -0400
783 @@ -373,13 +373,16 @@
784  
785  static inline int restore_i387_fxsave( struct _fpstate *buf )
786  {
787 +       int err;
788         struct task_struct *tsk = current;
789         clear_fpu( tsk );
790 -       if ( __copy_from_user( &tsk->thread.i387.fxsave, &buf->_fxsr_env[0],
791 -                              sizeof(struct i387_fxsave_struct) ) )
792 -               return 1;
793 +       err = __copy_from_user( &tsk->thread.i387.fxsave, &buf->_fxsr_env[0],
794 +                              sizeof(struct i387_fxsave_struct) );
795         /* mxcsr bit 6 and 31-16 must be zero for security reasons */
796         tsk->thread.i387.fxsave.mxcsr &= 0xffbf;
797 +       if (err)
798 +               return 1;
799 +
800         return convert_fxsr_from_user( &tsk->thread.i387.fxsave, buf );
801  }
802  
803 diff -urN linux-2.4.21/arch/i386/kernel/ioport.c linux-2.4.21/arch/i386/kernel/ioport.c
804 --- linux-2.4.21/arch/i386/kernel/ioport.c      2003-06-23 11:42:23.000000000 -0400
805 +++ linux-2.4.21/arch/i386/kernel/ioport.c      2003-06-23 11:49:15.000000000 -0400
806 @@ -14,6 +14,7 @@
807  #include <linux/smp.h>
808  #include <linux/smp_lock.h>
809  #include <linux/stddef.h>
810 +#include <linux/grsecurity.h>
811  
812  /* Set EXTENT bits starting at BASE in BITMAP to value TURN_ON. */
813  static void set_bitmap(unsigned long *bitmap, short base, short extent, int new_value)
814 @@ -59,8 +60,16 @@
815  
816         if ((from + num <= from) || (from + num > IO_BITMAP_SIZE*32))
817                 return -EINVAL;
818 +#ifdef CONFIG_GRKERNSEC_IO
819 +       if (turn_on) {
820 +               gr_handle_ioperm();
821 +#else
822         if (turn_on && !capable(CAP_SYS_RAWIO))
823 +#endif
824                 return -EPERM;
825 +#ifdef CONFIG_GRKERNSEC_IO
826 +       }
827 +#endif
828         /*
829          * If it's the first ioperm() call in this thread's lifetime, set the
830          * IO bitmap up. ioperm() is much less timing critical than clone(),
831 @@ -109,8 +118,13 @@
832                 return -EINVAL;
833         /* Trying to gain more privileges? */
834         if (level > old) {
835 +#ifdef CONFIG_GRKERNSEC_IO
836 +               gr_handle_iopl();
837 +               return -EPERM;
838 +#else
839                 if (!capable(CAP_SYS_RAWIO))
840                         return -EPERM;
841 +#endif
842         }
843         regs->eflags = (regs->eflags & 0xffffcfff) | (level << 12);
844         return 0;
845 diff -urN linux-2.4.21/arch/i386/kernel/ptrace.c linux-2.4.21/arch/i386/kernel/ptrace.c
846 --- linux-2.4.21/arch/i386/kernel/ptrace.c      2003-06-23 11:42:24.000000000 -0400
847 +++ linux-2.4.21/arch/i386/kernel/ptrace.c      2003-06-23 11:49:15.000000000 -0400
848 @@ -13,6 +13,7 @@
849  #include <linux/errno.h>
850  #include <linux/ptrace.h>
851  #include <linux/user.h>
852 +#include <linux/grsecurity.h>
853  
854  #include <asm/uaccess.h>
855  #include <asm/pgtable.h>
856 @@ -177,6 +178,9 @@
857         if (pid == 1)           /* you may not mess with init */
858                 goto out_tsk;
859  
860 +       if(gr_handle_ptrace(child, request))
861 +               goto out_tsk;
862 +
863         if (request == PTRACE_ATTACH) {
864                 ret = ptrace_attach(child);
865                 goto out_tsk;
866 @@ -256,6 +260,17 @@
867                           if(addr < (long) &dummy->u_debugreg[4] &&
868                              ((unsigned long) data) >= TASK_SIZE-3) break;
869                           
870 +#ifdef CONFIG_GRKERNSEC
871 +                         if(addr >= (long) &dummy->u_debugreg[0] &&
872 +                            addr <= (long) &dummy->u_debugreg[3]){
873 +                                 long reg   = (addr - (long) &dummy->u_debugreg[0]) >> 2;
874 +                                 long type  = (child->thread.debugreg[7] >> (DR_CONTROL_SHIFT + 4*reg)) & 3;
875 +                                 long align = (child->thread.debugreg[7] >> (DR_CONTROL_SHIFT + 2 + 4*reg)) & 3;
876 +                                 if((type & 1) && (data & align))
877 +                                       break;
878 +                         }
879 +#endif
880 +
881                           if(addr == (long) &dummy->u_debugreg[7]) {
882                                   data &= ~DR_CONTROL_RESERVED;
883                                   for(i=0; i<4; i++)
884 diff -urN linux-2.4.21/arch/i386/kernel/sys_i386.c linux-2.4.21/arch/i386/kernel/sys_i386.c
885 --- linux-2.4.21/arch/i386/kernel/sys_i386.c    2003-06-23 11:42:24.000000000 -0400
886 +++ linux-2.4.21/arch/i386/kernel/sys_i386.c    2003-06-23 11:49:15.000000000 -0400
887 @@ -18,6 +18,7 @@
888  #include <linux/mman.h>
889  #include <linux/file.h>
890  #include <linux/utsname.h>
891 +#include <linux/grsecurity.h>
892  
893  #include <asm/uaccess.h>
894  #include <asm/ipc.h>
895 @@ -55,8 +61,14 @@
896                         goto out;
897         }
898  
899 +       if(gr_handle_mmap(file, prot)) {
900 +               fput(file);
901 +               error = -EACCES;
902 +               goto out;
903 +       }
904 +
905         down_write(&mm->mmap_sem);
906         error = do_mmap_pgoff(mm, file, addr, len, prot, flags, pgoff);
907         up_write(&mm->mmap_sem);
908  
909         if (file)
910 diff -urN linux-2.4.21/arch/ia64/config.in linux-2.4.21/arch/ia64/config.in
911 --- linux-2.4.21/arch/ia64/config.in    2003-06-23 11:42:33.000000000 -0400
912 +++ linux-2.4.21/arch/ia64/config.in    2003-06-23 11:49:15.000000000 -0400
913 @@ -290,3 +290,12 @@
914  fi
915  
916  endmenu
917 +
918 +mainmenu_option next_comment
919 +comment 'Grsecurity'
920 +bool 'Grsecurity' CONFIG_GRKERNSEC
921 +if [ "$CONFIG_GRKERNSEC" = "y" ]; then
922 +    source grsecurity/Config.in
923 +fi
924 +endmenu
925 +
926 diff -urN linux-2.4.21/arch/ia64/kernel/ptrace.c linux-2.4.21/arch/ia64/kernel/ptrace.c
927 --- linux-2.4.21/arch/ia64/kernel/ptrace.c      2003-06-23 11:42:33.000000000 -0400
928 +++ linux-2.4.21/arch/ia64/kernel/ptrace.c      2003-06-23 11:49:15.000000000 -0400
929 @@ -16,6 +16,7 @@
930  #include <linux/ptrace.h>
931  #include <linux/smp_lock.h>
932  #include <linux/user.h>
933 +#include <linux/grsecurity.h>
934  
935  #include <asm/pgtable.h>
936  #include <asm/processor.h>
937 @@ -1179,6 +1180,9 @@
938         if (pid == 1)           /* no messing around with init! */
939                 goto out_tsk;
940  
941 +       if (gr_handle_ptrace(child, request))
942 +               goto out_tsk;
943 +
944         if (request == PTRACE_ATTACH) {
945                 ret = ptrace_attach(child);
946                 goto out_tsk;
947 diff -urN linux-2.4.21/arch/ia64/kernel/sys_ia64.c linux-2.4.21/arch/ia64/kernel/sys_ia64.c
948 --- linux-2.4.21/arch/ia64/kernel/sys_ia64.c    2003-06-23 11:42:33.000000000 -0400
949 +++ linux-2.4.21/arch/ia64/kernel/sys_ia64.c    2003-06-23 11:49:15.000000000 -0400
950 @@ -15,6 +15,7 @@
951  #include <linux/smp.h>
952  #include <linux/smp_lock.h>
953  #include <linux/highuid.h>
954 +#include <linux/grsecurity.h>
955  
956  #include <asm/shmparam.h>
957  #include <asm/uaccess.h>
958 @@ -212,6 +213,11 @@
959                 goto out;
960         }
961  
962 +       if (gr_handle_mmap(file, prot)) {
963 +               addr = -EACCES;
964 +               goto out;
965 +       }
966 +
967         down_write(&current->mm->mmap_sem);
968         addr = do_mmap_pgoff(current->mm, file, addr, len, prot, flags, pgoff);
969         up_write(&current->mm->mmap_sem);
970 diff -urN linux-2.4.21/arch/m68k/config.in linux-2.4.21/arch/m68k/config.in
971 --- linux-2.4.21/arch/m68k/config.in    2003-06-23 11:42:29.000000000 -0400
972 +++ linux-2.4.21/arch/m68k/config.in    2003-06-23 11:49:15.000000000 -0400
973 @@ -563,3 +563,11 @@
974  endmenu
975  
976  source lib/Config.in
977 +
978 +mainmenu_option next_comment
979 +comment 'Grsecurity'
980 +bool 'Grsecurity' CONFIG_GRKERNSEC
981 +if [ "$CONFIG_GRKERNSEC" = "y" ]; then
982 +       source grsecurity/Config.in
983 +fi
984 +endmenu
985 diff -urN linux-2.4.21/arch/mips/config.in linux-2.4.21/arch/mips/config.in
986 --- linux-2.4.21/arch/mips/config.in    2003-06-23 11:42:24.000000000 -0400
987 +++ linux-2.4.21/arch/mips/config.in    2003-06-23 11:49:15.000000000 -0400
988 @@ -7,3 +7,11 @@
989  define_bool CONFIG_MIPS64 n
990  
991  source arch/mips/config-shared.in
992 +
993 +mainmenu_option next_comment
994 +comment 'Grsecurity'
995 +bool 'Grsecurity' CONFIG_GRKERNSEC
996 +if [ "$CONFIG_GRKERNSEC" = "y" ]; then
997 +        source grsecurity/Config.in
998 +fi
999 +endmenu
1000 diff -urN linux-2.4.21/arch/mips64/config.in linux-2.4.21/arch/mips64/config.in
1001 --- linux-2.4.21/arch/mips64/config.in  2003-06-23 11:42:33.000000000 -0400
1002 +++ linux-2.4.21/arch/mips64/config.in  2003-06-23 11:49:15.000000000 -0400
1003 @@ -7,3 +7,11 @@
1004  define_bool CONFIG_MIPS64 y
1005  
1006  source arch/mips/config-shared.in
1007 +
1008 +mainmenu_option next_comment
1009 +comment 'Grsecurity'
1010 +bool 'Grsecurity' CONFIG_GRKERNSEC
1011 +if [ "$CONFIG_GRKERNSEC" = "y" ]; then
1012 +        source grsecurity/Config.in
1013 +fi
1014 +endmenu
1015 diff -urN linux-2.4.21/arch/parisc/config.in linux-2.4.21/arch/parisc/config.in
1016 --- linux-2.4.21/arch/parisc/config.in  2003-06-23 11:42:33.000000000 -0400
1017 +++ linux-2.4.21/arch/parisc/config.in  2003-06-23 11:49:15.000000000 -0400
1018 @@ -197,3 +197,11 @@
1019  endmenu
1020  
1021  source lib/Config.in
1022 +
1023 +mainmenu_option next_comment
1024 +comment 'Grsecurity'
1025 +bool 'Grsecurity' CONFIG_GRKERNSEC
1026 +if [ "$CONFIG_GRKERNSEC" = "y" ]; then
1027 +       source grsecurity/Config.in
1028 +fi
1029 +endmenu
1030 diff -urN linux-2.4.21/arch/parisc/kernel/ioctl32.c linux-2.4.21/arch/parisc/kernel/ioctl32.c
1031 --- linux-2.4.21/arch/parisc/kernel/ioctl32.c   2003-06-23 11:42:33.000000000 -0400
1032 +++ linux-2.4.21/arch/parisc/kernel/ioctl32.c   2003-06-23 11:49:15.000000000 -0400
1033 @@ -1434,7 +1434,11 @@
1034          * To have permissions to do most of the vt ioctls, we either have
1035          * to be the owner of the tty, or super-user.
1036          */
1037 +#ifdef CONFIG_GRKERNSEC
1038 +       if (current->tty == tty || capable(CAP_SYS_TTY_CONFIG))
1039 +#else
1040         if (current->tty == tty || suser())
1041 +#endif
1042                 return 1;
1043         return 0;                                                    
1044  }
1045 diff -urN linux-2.4.21/arch/parisc/kernel/ptrace.c linux-2.4.21/arch/parisc/kernel/ptrace.c
1046 --- linux-2.4.21/arch/parisc/kernel/ptrace.c    2003-06-23 11:42:34.000000000 -0400
1047 +++ linux-2.4.21/arch/parisc/kernel/ptrace.c    2003-06-23 11:49:15.000000000 -0400
1048 @@ -15,7 +15,7 @@
1049  #include <linux/ptrace.h>
1050  #include <linux/user.h>
1051  #include <linux/personality.h>
1052 -
1053 +#include <linux/grsecurity.h>
1054  #include <asm/uaccess.h>
1055  #include <asm/pgtable.h>
1056  #include <asm/system.h>
1057 @@ -119,6 +119,9 @@
1058         if (pid == 1)           /* no messing around with init! */
1059                 goto out_tsk;
1060  
1061 +       if (gr_handle_ptrace(child, request))
1062 +               goto out_tsk;
1063 +
1064         if (request == PTRACE_ATTACH) {
1065                 ret = ptrace_attach(child);
1066                 goto out_tsk;
1067 diff -urN linux-2.4.21/arch/parisc/kernel/sys_parisc.c linux-2.4.21/arch/parisc/kernel/sys_parisc.c
1068 --- linux-2.4.21/arch/parisc/kernel/sys_parisc.c        2003-06-23 11:42:33.000000000 -0400
1069 +++ linux-2.4.21/arch/parisc/kernel/sys_parisc.c        2003-06-23 11:49:15.000000000 -0400
1070 @@ -12,6 +12,7 @@
1071  #include <linux/mman.h>
1072  #include <linux/shm.h>
1073  #include <linux/smp_lock.h>
1074 +#include <linux/grsecurity.h>
1075  
1076  int sys_pipe(int *fildes)
1077  {
1078 @@ -104,6 +110,11 @@
1079                         goto out;
1080         }
1081  
1082 +       if (gr_handle_mmap(file, prot)) {
1083 +               fput(file);
1084 +               return -EACCES;
1085 +       }
1086 +
1087         flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
1088  
1089         down_write(&current->mm->mmap_sem);
1090 diff -urN linux-2.4.21/arch/parisc/kernel/sys_parisc32.c linux-2.4.21/arch/parisc/kernel/sys_parisc32.c
1091 --- linux-2.4.21/arch/parisc/kernel/sys_parisc32.c      2003-06-23 11:42:33.000000000 -0400
1092 +++ linux-2.4.21/arch/parisc/kernel/sys_parisc32.c      2003-06-23 11:49:15.000000000 -0400
1093 @@ -50,6 +50,7 @@
1094  #include <linux/highmem.h>
1095  #include <linux/highuid.h>
1096  #include <linux/mman.h>
1097 +#include <linux/grsecurity.h>
1098  
1099  #include <asm/types.h>
1100  #include <asm/uaccess.h>
1101 @@ -177,6 +178,11 @@
1102         struct file *file;
1103         int retval;
1104         int i;
1105 +#ifdef CONFIG_GRKERNSEC
1106 +       struct file *old_exec_file;
1107 +       struct acl_subject_label *old_acl;
1108 +       struct rlimit old_rlim[RLIM_NLIMITS];
1109 +#endif
1110  
1111         file = open_exec(filename);
1112  
1113 @@ -184,6 +190,20 @@
1114         if (IS_ERR(file))
1115                 return retval;
1116  
1117 +       gr_learn_resource(current, RLIMIT_NPROC, atomic_read(&current->user->processes));
1118 +
1119 +       if (gr_handle_nproc()) {
1120 +               allow_write_access(file);
1121 +               fput(file);
1122 +               return -EAGAIN;
1123 +       }
1124 +
1125 +       if (!gr_acl_handle_execve(file->f_dentry, file->f_vfsmnt)) {
1126 +               allow_write_access(file);
1127 +               fput(file);
1128 +               return -EACCES;
1129 +       }
1130 +
1131         bprm.p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1132         memset(bprm.page, 0, MAX_ARG_PAGES*sizeof(bprm.page[0]));
1133  
1134 @@ -209,11 +238,24 @@
1135         if (retval < 0)
1136                 goto out;
1137         
1138 +       if (!gr_tpe_allow(file)) {
1139 +               retval = -EACCES;
1140 +               goto out;
1141 +       }
1142 +
1143 +       if (gr_check_crash_exec(file)) {
1144 +               retval = -EACCES;
1145 +               goto out;
1146 +       }
1147 +
1148         retval = copy_strings_kernel(1, &bprm.filename, &bprm);
1149         if (retval < 0)
1150                 goto out;
1151  
1152         bprm.exec = bprm.p;
1153 +
1154 +       gr_log_chroot_exec(file->f_dentry, file->f_vfsmnt);
1155 +
1156         retval = copy_strings32(bprm.envc, envp, &bprm);
1157         if (retval < 0)
1158                 goto out;
1159 @@ -222,11 +264,31 @@
1160         if (retval < 0)
1161                 goto out;
1162  
1163 +#ifdef CONFIG_GRKERNSEC
1164 +       old_acl = current->acl;
1165 +       memcpy(old_rlim, current->rlim, sizeof(old_rlim));
1166 +       old_exec_file = current->exec_file;
1167 +       get_file(file);
1168 +       current->exec_file = file;
1169 +#endif
1170 +
1171 +       gr_set_proc_label(file->f_dentry, file->f_vfsmnt);
1172 +
1173         retval = search_binary_handler(&bprm,regs);
1174 -       if (retval >= 0)
1175 +       if (retval >= 0) {
1176 +#ifdef CONFIG_GRKERNSEC
1177 +               if (old_exec_file)
1178 +                       fput(old_exec_file);
1179 +#endif
1180                 /* execve success */
1181                 return retval;
1182  
1183 +#ifdef CONFIG_GRKERNSEC
1184 +       current->acl = old_acl;
1185 +       memcpy(current->rlim, old_rlim, sizeof(old_rlim));
1186 +       fput(current->exec_file);
1187 +       current->exec_file = old_exec_file;
1188 +#endif
1189  out:
1190         /* Something went wrong, return the inode and free the argument pages*/
1191         allow_write_access(bprm.file);
1192 diff -urN linux-2.4.21/arch/ppc/config.in linux-2.4.21/arch/ppc/config.in
1193 --- linux-2.4.21/arch/ppc/config.in     2003-06-23 11:42:28.000000000 -0400
1194 +++ linux-2.4.21/arch/ppc/config.in     2003-06-23 11:49:15.000000000 -0400
1195 @@ -461,3 +461,12 @@
1196    bool 'Support for early boot texts over serial port' CONFIG_SERIAL_TEXT_DEBUG
1197  fi
1198  endmenu
1199 +
1200 +mainmenu_option next_comment
1201 +comment 'Grsecurity'
1202 +bool 'Grsecurity' CONFIG_GRKERNSEC
1203 +if [ "$CONFIG_GRKERNSEC" = "y" ]; then
1204 +    source grsecurity/Config.in
1205 +fi
1206 +endmenu
1207 +
1208 diff -urN linux-2.4.21/arch/ppc/kernel/ptrace.c linux-2.4.21/arch/ppc/kernel/ptrace.c
1209 --- linux-2.4.21/arch/ppc/kernel/ptrace.c       2003-06-23 11:42:28.000000000 -0400
1210 +++ linux-2.4.21/arch/ppc/kernel/ptrace.c       2003-06-23 11:49:15.000000000 -0400
1211 @@ -24,6 +24,7 @@
1212  #include <linux/errno.h>
1213  #include <linux/ptrace.h>
1214  #include <linux/user.h>
1215 +#include <linux/grsecurity.h>
1216  
1217  #include <asm/uaccess.h>
1218  #include <asm/page.h>
1219 @@ -182,6 +183,9 @@
1220         if (pid == 1)           /* you may not mess with init */
1221                 goto out_tsk;
1222  
1223 +       if (gr_handle_ptrace(child, request))
1224 +               goto out_tsk;
1225 +
1226         if (request == PTRACE_ATTACH) {
1227                 ret = ptrace_attach(child);
1228                 goto out_tsk;
1229 diff -urN linux-2.4.21/arch/ppc/kernel/syscalls.c linux-2.4.21/arch/ppc/kernel/syscalls.c
1230 --- linux-2.4.21/arch/ppc/kernel/syscalls.c     2003-06-23 11:42:28.000000000 -0400
1231 +++ linux-2.4.21/arch/ppc/kernel/syscalls.c     2003-06-23 11:49:15.000000000 -0400
1232 @@ -35,6 +35,7 @@
1233  #include <linux/ipc.h>
1234  #include <linux/utsname.h>
1235  #include <linux/file.h>
1236 +#include <linux/grsecurity.h>
1237  
1238  #include <asm/uaccess.h>
1239  #include <asm/ipc.h>
1240 @@ -181,8 +182,14 @@
1241                         goto out;
1242         }
1243         
1244 +       if (gr_handle_mmap(file, prot)) {
1245 +               fput(file);
1246 +               ret = -EACCES;
1247 +               goto out;
1248 +       }
1249 +
1250         down_write(&current->mm->mmap_sem);
1251         ret = do_mmap_pgoff(current->mm, file, addr, len, prot, flags, pgoff);
1252         up_write(&current->mm->mmap_sem);
1253         if (file)
1254                 fput(file);
1255 diff -urN linux-2.4.21/arch/ppc64/kernel/ioctl32.c linux-2.4.21/arch/ppc64/kernel/ioctl32.c
1256 --- linux-2.4.21/arch/ppc64/kernel/ioctl32.c    2003-06-23 11:42:23.000000000 -0400
1257 +++ linux-2.4.21/arch/ppc64/kernel/ioctl32.c    2003-06-23 11:49:15.000000000 -0400
1258 @@ -1789,7 +1789,11 @@
1259          * To have permissions to do most of the vt ioctls, we either have
1260          * to be the owner of the tty, or super-user.
1261          */
1262 +#ifdef CONFIG_GRKERNSEC
1263 +       if (current->tty == tty || capable(CAP_SYS_TTY_CONFIG))
1264 +#else
1265         if (current->tty == tty || suser())
1266 +#endif
1267                 return 1;
1268         return 0;                                                    
1269  }
1270 diff -urN linux-2.4.21/arch/s390/config.in linux-2.4.21/arch/s390/config.in
1271 --- linux-2.4.21/arch/s390/config.in    2003-06-23 11:42:33.000000000 -0400
1272 +++ linux-2.4.21/arch/s390/config.in    2003-06-23 11:49:15.000000000 -0400
1273 @@ -76,3 +76,11 @@
1274  endmenu
1275  
1276  source lib/Config.in
1277 +
1278 +mainmenu_option next_comment
1279 +comment 'Grsecurity'
1280 +bool 'Grsecurity' CONFIG_GRKERNSEC
1281 +if [ "$CONFIG_GRKERNSEC" = "y" ]; then
1282 +       source grsecurity/Config.in
1283 +fi
1284 +endmenu
1285 diff -urN linux-2.4.21/arch/s390x/config.in linux-2.4.21/arch/s390x/config.in
1286 --- linux-2.4.21/arch/s390x/config.in   2003-06-23 11:42:34.000000000 -0400
1287 +++ linux-2.4.21/arch/s390x/config.in   2003-06-23 11:49:15.000000000 -0400
1288 @@ -80,3 +80,11 @@
1289  endmenu
1290  
1291  source lib/Config.in
1292 +
1293 +mainmenu_option next_comment
1294 +comment 'Grsecurity'
1295 +bool 'Grsecurity' CONFIG_GRKERNSEC
1296 +if [ "$CONFIG_GRKERNSEC" = "y" ]; then
1297 +       source grsecurity/Config.in
1298 +fi
1299 +endmenu
1300 diff -urN linux-2.4.21/arch/sh/config.in linux-2.4.21/arch/sh/config.in
1301 --- linux-2.4.21/arch/sh/config.in      2003-06-23 11:42:32.000000000 -0400
1302 +++ linux-2.4.21/arch/sh/config.in      2003-06-23 11:49:15.000000000 -0400
1303 @@ -388,3 +388,11 @@
1304  endmenu
1305  
1306  source lib/Config.in
1307 +
1308 +mainmenu_option next_comment
1309 +comment 'Grsecurity'
1310 +bool 'Grsecurity' CONFIG_GRKERNSEC
1311 +if [ "$CONFIG_GRKERNSEC" = "y" ]; then
1312 +       source grsecurity/Config.in
1313 +fi
1314 +endmenu
1315 diff -urN linux-2.4.21/arch/sparc/boot/Makefile linux-2.4.21/arch/sparc/boot/Makefile
1316 --- linux-2.4.21/arch/sparc/boot/Makefile       2003-06-23 11:42:24.000000000 -0400
1317 +++ linux-2.4.21/arch/sparc/boot/Makefile       2003-06-23 11:49:15.000000000 -0400
1318 @@ -24,7 +24,7 @@
1319  
1320  BTOBJS := $(HEAD) init/main.o init/version.o init/do_mounts.o
1321  BTLIBS := $(CORE_FILES_NO_BTFIX) $(FILESYSTEMS) \
1322 -       $(DRIVERS) $(NETWORKS)
1323 +       $(DRIVERS) $(NETWORKS) $(GRSECURITY)
1324  
1325  # I wanted to make this depend upon BTOBJS so that a parallel
1326  # build would work, but this fails because $(HEAD) cannot work
1327 diff -urN linux-2.4.21/arch/sparc/config.in linux-2.4.21/arch/sparc/config.in
1328 --- linux-2.4.21/arch/sparc/config.in   2003-06-23 11:42:24.000000000 -0400
1329 +++ linux-2.4.21/arch/sparc/config.in   2003-06-23 11:49:15.000000000 -0400
1330 @@ -276,3 +276,11 @@
1331  endmenu
1332  
1333  source lib/Config.in
1334 +
1335 +mainmenu_option next_comment
1336 +comment 'Grsecurity'
1337 +bool 'Grsecurity' CONFIG_GRKERNSEC
1338 +if [ "$CONFIG_GRKERNSEC" = "y" ]; then
1339 +       source grsecurity/Config.in
1340 +fi
1341 +endmenu
1342 diff -urN linux-2.4.21/arch/sparc/kernel/ptrace.c linux-2.4.21/arch/sparc/kernel/ptrace.c
1343 --- linux-2.4.21/arch/sparc/kernel/ptrace.c     2003-06-23 11:42:24.000000000 -0400
1344 +++ linux-2.4.21/arch/sparc/kernel/ptrace.c     2003-06-23 11:49:15.000000000 -0400
1345 @@ -17,6 +17,7 @@
1346  #include <linux/user.h>
1347  #include <linux/smp.h>
1348  #include <linux/smp_lock.h>
1349 +#include <linux/grsecurity.h>
1350  
1351  #include <asm/pgtable.h>
1352  #include <asm/system.h>
1353 @@ -310,6 +311,9 @@
1354                 goto out;
1355         }
1356  
1357 +       if(gr_handle_ptrace(child, request))
1358 +               goto out_tsk;
1359 +
1360         if ((current->personality == PER_SUNOS && request == PTRACE_SUNATTACH)
1361             || (current->personality != PER_SUNOS && request == PTRACE_ATTACH)) {
1362                 if (ptrace_attach(child)) {
1363 diff -urN linux-2.4.21/arch/sparc/kernel/sys_sparc.c linux-2.4.21/arch/sparc/kernel/sys_sparc.c
1364 --- linux-2.4.21/arch/sparc/kernel/sys_sparc.c  2003-06-23 11:42:24.000000000 -0400
1365 +++ linux-2.4.21/arch/sparc/kernel/sys_sparc.c  2003-06-23 11:49:15.000000000 -0400
1366 @@ -20,6 +20,7 @@
1367  #include <linux/utsname.h>
1368  #include <linux/smp.h>
1369  #include <linux/smp_lock.h>
1370 +#include <linux/grsecurity.h>
1371  
1372  #include <asm/uaccess.h>
1373  #include <asm/ipc.h>
1374 @@ -240,6 +253,12 @@
1375         if (len > TASK_SIZE - PAGE_SIZE || addr + len > TASK_SIZE - PAGE_SIZE)
1376                 goto out_putf;
1377  
1378 +       if (gr_handle_mmap(file, prot)) {
1379 +               fput(file);
1380 +               retval = -EACCES;
1381 +               goto out;
1382 +       }
1383 +
1384         flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
1385  
1386         down_write(&current->mm->mmap_sem);
1387 diff -urN linux-2.4.21/arch/sparc64/config.in linux-2.4.21/arch/sparc64/config.in
1388 --- linux-2.4.21/arch/sparc64/config.in 2003-06-23 11:42:30.000000000 -0400
1389 +++ linux-2.4.21/arch/sparc64/config.in 2003-06-23 11:49:15.000000000 -0400
1390 @@ -310,3 +310,11 @@
1391  endmenu
1392  
1393  source lib/Config.in
1394 +
1395 +mainmenu_option next_comment
1396 +comment 'Grsecurity'
1397 +bool 'Grsecurity' CONFIG_GRKERNSEC
1398 +if [ "$CONFIG_GRKERNSEC" = "y" ]; then
1399 +       source grsecurity/Config.in
1400 +fi
1401 +endmenu
1402 diff -urN linux-2.4.21/arch/sparc64/kernel/ioctl32.c linux-2.4.21/arch/sparc64/kernel/ioctl32.c
1403 --- linux-2.4.21/arch/sparc64/kernel/ioctl32.c  2003-06-23 11:42:30.000000000 -0400
1404 +++ linux-2.4.21/arch/sparc64/kernel/ioctl32.c  2003-06-23 11:49:15.000000000 -0400
1405 @@ -2046,7 +2046,11 @@
1406          * To have permissions to do most of the vt ioctls, we either have
1407          * to be the owner of the tty, or super-user.
1408          */
1409 +#ifdef CONFIG_GRKERNSEC
1410 +       if (current->tty == tty || capable(CAP_SYS_TTY_CONFIG))
1411 +#else
1412         if (current->tty == tty || suser())
1413 +#endif
1414                 return 1;
1415         return 0;                                                    
1416  }
1417 diff -urN linux-2.4.21/arch/sparc64/kernel/ptrace.c linux-2.4.21/arch/sparc64/kernel/ptrace.c
1418 --- linux-2.4.21/arch/sparc64/kernel/ptrace.c   2003-06-23 11:42:30.000000000 -0400
1419 +++ linux-2.4.21/arch/sparc64/kernel/ptrace.c   2003-06-23 11:49:15.000000000 -0400
1420 @@ -18,6 +18,7 @@
1421  #include <linux/user.h>
1422  #include <linux/smp.h>
1423  #include <linux/smp_lock.h>
1424 +#include <linux/grsecurity.h>
1425  
1426  #include <asm/asi.h>
1427  #include <asm/pgtable.h>
1428 @@ -161,6 +162,11 @@
1429                 goto out;
1430         }
1431  
1432 +       if (gr_handle_ptrace(child, (long)request)) {
1433 +               pt_error_return(regs, EPERM);
1434 +               goto out;
1435 +       }
1436 +
1437         if ((current->personality == PER_SUNOS && request == PTRACE_SUNATTACH)
1438             || (current->personality != PER_SUNOS && request == PTRACE_ATTACH)) {
1439                 if (ptrace_attach(child)) {
1440 diff -urN linux-2.4.21/arch/sparc64/kernel/sys_sparc.c linux-2.4.21/arch/sparc64/kernel/sys_sparc.c
1441 --- linux-2.4.21/arch/sparc64/kernel/sys_sparc.c        2003-06-23 11:42:30.000000000 -0400
1442 +++ linux-2.4.21/arch/sparc64/kernel/sys_sparc.c        2003-06-23 11:49:15.000000000 -0400
1443 @@ -24,6 +24,7 @@
1444  #include <linux/slab.h>
1445  #include <linux/ipc.h>
1446  #include <linux/personality.h>
1447 +#include <linux/grsecurity.h>
1448  
1449  #include <asm/uaccess.h>
1450  #include <asm/ipc.h>
1451 @@ -280,6 +288,12 @@
1452                 if (!file)
1453                         goto out;
1454         }
1455 +
1456 +       if (gr_handle_mmap(file, prot)) {
1457 +               retval = -EACCES;
1458 +               goto out_putf;
1459 +       }
1460 +
1461         flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
1462         len = PAGE_ALIGN(len);
1463         retval = -EINVAL;
1464 diff -urN linux-2.4.21/arch/sparc64/kernel/sys_sparc32.c linux-2.4.21/arch/sparc64/kernel/sys_sparc32.c
1465 --- linux-2.4.21/arch/sparc64/kernel/sys_sparc32.c      2003-06-23 11:42:30.000000000 -0400
1466 +++ linux-2.4.21/arch/sparc64/kernel/sys_sparc32.c      2003-06-23 11:49:15.000000000 -0400
1467 @@ -51,6 +51,8 @@
1468  #include <linux/sysctl.h>
1469  #include <linux/dnotify.h>
1470  #include <linux/netfilter_ipv4/ip_tables.h>
1471 +#include <linux/random.h>
1472 +#include <linux/grsecurity.h>
1473  
1474  #include <asm/types.h>
1475  #include <asm/ipc.h>
1476 @@ -3203,6 +3205,11 @@
1477         struct file * file;
1478         int retval;
1479         int i;
1480 +#ifdef CONFIG_GRKERNSEC
1481 +       struct file *old_exec_file;
1482 +       struct acl_subject_label *old_acl;
1483 +       struct rlimit old_rlim[RLIM_NLIMITS];
1484 +#endif
1485  
1486         bprm.p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1487         memset(bprm.page, 0, MAX_ARG_PAGES * sizeof(bprm.page[0]));
1488 @@ -3213,6 +3229,20 @@
1489         if (IS_ERR(file))
1490                 return retval;
1491  
1492 +       gr_learn_resource(current, RLIMIT_NPROC, atomic_read(&current->user->processes));
1493 +
1494 +       if (gr_handle_nproc()) {
1495 +               allow_write_access(file);
1496 +               fput(file);
1497 +               return -EAGAIN;
1498 +       }
1499 +
1500 +       if (!gr_acl_handle_execve(file->f_dentry, file->f_vfsmnt)) {
1501 +               allow_write_access(file);
1502 +               fput(file);
1503 +               return -EACCES;
1504 +       }
1505 +
1506         bprm.file = file;
1507         bprm.filename = filename;
1508         bprm.sh_bang = 0;
1509 @@ -3233,11 +3263,24 @@
1510         if (retval < 0)
1511                 goto out;
1512         
1513 +       if(!gr_tpe_allow(file)) {
1514 +               retval = -EACCES;
1515 +               goto out;
1516 +       }
1517 +
1518 +       if (gr_check_crash_exec(file)) {
1519 +               retval = -EACCES;
1520 +               goto out;
1521 +       }
1522 +
1523         retval = copy_strings_kernel(1, &bprm.filename, &bprm);
1524         if (retval < 0)
1525                 goto out;
1526  
1527         bprm.exec = bprm.p;
1528 +
1529 +       gr_log_chroot_exec(file->f_dentry, file->f_vfsmnt);
1530 +
1531         retval = copy_strings32(bprm.envc, envp, &bprm);
1532         if (retval < 0)
1533                 goto out;
1534 @@ -3246,11 +3289,32 @@
1535         if (retval < 0)
1536                 goto out;
1537  
1538 +#ifdef CONFIG_GRKERNSEC
1539 +       old_acl = current->acl;
1540 +       memcpy(old_rlim, current->rlim, sizeof(old_rlim));
1541 +       old_exec_file = current->exec_file;
1542 +       get_file(file);
1543 +       current->exec_file = file;
1544 +#endif
1545 +
1546 +        gr_set_proc_label(file->f_dentry, file->f_vfsmnt);
1547 +
1548         retval = search_binary_handler(&bprm, regs);
1549 -       if (retval >= 0)
1550 +       if (retval >= 0) {
1551 +#ifdef CONFIG_GRKERNSEC
1552 +               if (old_exec_file)
1553 +                       fput(old_exec_file);
1554 +#endif
1555                 /* execve success */
1556                 return retval;
1557 +       }
1558  
1559 +#ifdef CONFIG_GRKERNSEC
1560 +       current->acl = old_acl;
1561 +       memcpy(current->rlim, old_rlim, sizeof(old_rlim));
1562 +       fput(current->exec_file);
1563 +       current->exec_file = old_exec_file;
1564 +#endif
1565  out:
1566         /* Something went wrong, return the inode and free the argument pages*/
1567         allow_write_access(bprm.file);
1568 diff -urN linux-2.4.21/arch/x86_64/ia32/ia32_ioctl.c linux-2.4.21/arch/x86_64/ia32/ia32_ioctl.c
1569 --- linux-2.4.21/arch/x86_64/ia32/ia32_ioctl.c  2003-06-23 11:42:23.000000000 -0400
1570 +++ linux-2.4.21/arch/x86_64/ia32/ia32_ioctl.c  2003-06-23 11:49:15.000000000 -0400
1571 @@ -1932,7 +1932,11 @@
1572          * To have permissions to do most of the vt ioctls, we either have
1573          * to be the owner of the tty, or super-user.
1574          */
1575 +#ifdef CONFIG_GRKERNSEC
1576 +       if (current->tty == tty || capable(CAP_SYS_TTY_CONFIG))
1577 +#else
1578         if (current->tty == tty || suser())
1579 +#endif
1580                 return 1;
1581         return 0;                                                    
1582  }
1583 diff -urN linux-2.4.21/drivers/char/keyboard.c linux-2.4.21/drivers/char/keyboard.c
1584 --- linux-2.4.21/drivers/char/keyboard.c        2003-07-29 17:57:21.000000000 -0400
1585 +++ linux-2.4.21/drivers/char/keyboard.c        2003-07-29 17:58:19.000000000 -0400
1586 @@ -549,6 +549,16 @@
1587         if ((kbd->kbdmode == VC_RAW || kbd->kbdmode == VC_MEDIUMRAW) &&
1588             !(SPECIALS_ALLOWED_IN_RAW_MODE & (1 << value)))
1589                 return;
1590 +
1591 +#if defined(CONFIG_GRKERNSEC_PROC) || defined(CONFIG_GRKERNSEC_PROC_MEMMAP)
1592 +       {
1593 +               void *func = spec_fn_table[value];
1594 +               if (func == show_state || func == show_ptregs ||
1595 +                   func == show_mem)
1596 +                       return;
1597 +       }
1598 +#endif
1599 +
1600         spec_fn_table[value]();
1601  }
1602  
1603 diff -urN linux-2.4.21/drivers/char/mem.c linux-2.4.21/drivers/char/mem.c
1604 --- linux-2.4.21/drivers/char/mem.c     2003-07-29 17:57:21.000000000 -0400
1605 +++ linux-2.4.21/drivers/char/mem.c     2003-07-29 17:58:19.000000000 -0400
1606 @@ -21,6 +21,7 @@
1607  #include <linux/raw.h>
1608  #include <linux/tty.h>
1609  #include <linux/capability.h>
1610 +#include <linux/grsecurity.h>
1611  
1612  #include <asm/uaccess.h>
1613  #include <asm/io.h>
1614 @@ -41,6 +42,10 @@
1615  #if defined(CONFIG_S390_TAPE) && defined(CONFIG_S390_TAPE_CHAR)
1616  extern void tapechar_init(void);
1617  #endif
1618 +
1619 +#ifdef CONFIG_GRKERNSEC
1620 +extern struct file_operations grsec_fops;
1621 +#endif
1622       
1623  static ssize_t do_write_mem(struct file * file, void *p, unsigned long realp,
1624                             const char * buf, size_t count, loff_t *ppos)
1625 @@ -114,6 +119,11 @@
1626         unsigned long p = *ppos;
1627         unsigned long end_mem;
1628  
1629 +#ifdef CONFIG_GRKERNSEC_KMEM
1630 +       gr_handle_mem_write();
1631 +       return -EPERM;
1632 +#endif
1633 +
1634         end_mem = __pa(high_memory);
1635         if (p >= end_mem)
1636                 return 0;
1637 @@ -186,6 +196,12 @@
1638  {
1639         unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
1640  
1641 +#ifdef CONFIG_GRKERNSEC_KMEM
1642 +       if (gr_handle_mem_mmap(offset, vma))
1643 +               return -EPERM;
1644 +#endif
1645 +
1646 +
1647         /*
1648          * Accessing memory above the top the kernel knows about or
1649          * through a file pointer that was marked O_SYNC will be
1650 @@ -285,6 +301,11 @@
1651         ssize_t virtr = 0;
1652         char * kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
1653  
1654 +#ifdef CONFIG_GRKERNSEC_KMEM
1655 +       gr_handle_kmem_write();
1656 +       return -EPERM;
1657 +#endif
1658 +
1659         if (p < (unsigned long) high_memory) {
1660                 wrote = count;
1661                 if (count > (unsigned long) high_memory - p)
1662 @@ -517,6 +538,15 @@
1663  
1664  static int open_port(struct inode * inode, struct file * filp)
1665  {
1666 +#ifdef CONFIG_GRKERNSEC_KMEM
1667 +       gr_handle_open_port();
1668 +       return -EPERM;
1669 +#endif
1670 +       return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
1671 +}
1672 +
1673 +static int open_mem(struct inode * inode, struct file * filp)
1674 +{
1675         return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
1676  }
1677  
1678 @@ -609,7 +639,6 @@
1679  #define full_lseek      null_lseek
1680  #define write_zero     write_null
1681  #define read_full       read_zero
1682 -#define open_mem       open_port
1683  #define open_kmem      open_mem
1684  
1685  static struct file_operations mem_fops = {
1686 @@ -685,6 +714,11 @@
1687                 case 9:
1688                         filp->f_op = &urandom_fops;
1689                         break;
1690 +#ifdef CONFIG_GRKERNSEC
1691 +               case 10:
1692 +                       filp->f_op = &grsec_fops;
1693 +                       break;
1694 +#endif
1695                 default:
1696                         return -ENXIO;
1697         }
1698 @@ -711,7 +745,10 @@
1699         {5, "zero",    S_IRUGO | S_IWUGO,           &zero_fops},
1700         {7, "full",    S_IRUGO | S_IWUGO,           &full_fops},
1701         {8, "random",  S_IRUGO | S_IWUSR,           &random_fops},
1702 -       {9, "urandom", S_IRUGO | S_IWUSR,           &urandom_fops}
1703 +       {9, "urandom", S_IRUGO | S_IWUSR,           &urandom_fops},
1704 +#ifdef CONFIG_GRKERNSEC
1705 +       {10,"grsec",   S_IRUSR | S_IWUGO,           &grsec_fops}
1706 +#endif
1707      };
1708      int i;
1709  
1710 diff -urN linux-2.4.21/drivers/char/random.c linux-2.4.21/drivers/char/random.c
1711 --- linux-2.4.21/drivers/char/random.c  2003-06-23 11:42:01.000000000 -0400
1712 +++ linux-2.4.21/drivers/char/random.c  2003-06-23 11:49:15.000000000 -0400
1713 @@ -260,9 +260,15 @@
1714  /*
1715   * Configuration information
1716   */
1717 +#ifdef CONFIG_GRKERNSEC_RANDNET
1718 +#define DEFAULT_POOL_SIZE 1024
1719 +#define SECONDARY_POOL_SIZE 256
1720 +#define BATCH_ENTROPY_SIZE 512
1721 +#else
1722  #define DEFAULT_POOL_SIZE 512
1723  #define SECONDARY_POOL_SIZE 128
1724  #define BATCH_ENTROPY_SIZE 256
1725 +#endif
1726  #define USE_SHA
1727  
1728  /*
1729 @@ -387,6 +393,7 @@
1730  /*
1731   * Static global variables
1732   */
1733 +
1734  static struct entropy_store *random_state; /* The default global store */
1735  static struct entropy_store *sec_random_state; /* secondary store */
1736  static DECLARE_WAIT_QUEUE_HEAD(random_read_wait);
1737 diff -urN linux-2.4.21/drivers/char/tty_io.c linux-2.4.21/drivers/char/tty_io.c
1738 --- linux-2.4.21/drivers/char/tty_io.c  2003-06-23 11:42:01.000000000 -0400
1739 +++ linux-2.4.21/drivers/char/tty_io.c  2003-06-23 11:49:15.000000000 -0400
1740 @@ -99,7 +99,7 @@
1741  #include <linux/vt_kern.h>
1742  #include <linux/selection.h>
1743  #include <linux/devfs_fs_kernel.h>
1744 -
1745 +#include <linux/grsecurity.h>
1746  #include <linux/kmod.h>
1747  
1748  #ifdef CONFIG_VT
1749 @@ -1397,7 +1397,11 @@
1750                 retval = -ENODEV;
1751         filp->f_flags = saved_flags;
1752  
1753 +#ifdef CONFIG_GRKERNSEC
1754 +       if (!retval && test_bit(TTY_EXCLUSIVE, &tty->flags) && !capable(CAP_SYS_TTY_CONFIG))
1755 +#else
1756         if (!retval && test_bit(TTY_EXCLUSIVE, &tty->flags) && !suser())
1757 +#endif
1758                 retval = -EBUSY;
1759  
1760         if (retval) {
1761 @@ -1499,7 +1503,11 @@
1762  {
1763         char ch, mbz = 0;
1764  
1765 +#ifdef CONFIG_GRKERNSEC
1766 +       if ((current->tty != tty) && !capable(CAP_SYS_TTY_CONFIG))
1767 +#else
1768         if ((current->tty != tty) && !suser())
1769 +#endif
1770                 return -EPERM;
1771         if (get_user(ch, arg))
1772                 return -EFAULT;
1773 @@ -1537,7 +1545,11 @@
1774         if (inode->i_rdev == SYSCONS_DEV ||
1775             inode->i_rdev == CONSOLE_DEV) {
1776                 struct file *f;
1777 +#ifdef CONFIG_GRKERNSEC
1778 +               if (!capable(CAP_SYS_TTY_CONFIG))
1779 +#else
1780                 if (!suser())
1781 +#endif
1782                         return -EPERM;
1783                 spin_lock(&redirect_lock);
1784                 f = redirect;
1785 @@ -1589,7 +1601,11 @@
1786                  * This tty is already the controlling
1787                  * tty for another session group!
1788                  */
1789 +#ifdef CONFIG_GRKERNSEC
1790 +               if ((arg == 1) && capable(CAP_SYS_ADMIN)) {
1791 +#else
1792                 if ((arg == 1) && suser()) {
1793 +#endif
1794                         /*
1795                          * Steal it away
1796                          */
1797 diff -urN linux-2.4.21/drivers/char/vt.c linux-2.4.21/drivers/char/vt.c
1798 --- linux-2.4.21/drivers/char/vt.c      2003-06-23 11:42:01.000000000 -0400
1799 +++ linux-2.4.21/drivers/char/vt.c      2003-06-23 11:49:15.000000000 -0400
1800 @@ -32,6 +32,7 @@
1801  #include <linux/vt_kern.h>
1802  #include <linux/kbd_diacr.h>
1803  #include <linux/selection.h>
1804 +#include <linux/grsecurity.h>
1805  
1806  #ifdef CONFIG_FB_COMPAT_XPMAC
1807  #include <asm/vc_ioctl.h>
1808 @@ -443,7 +444,11 @@
1809          * to be the owner of the tty, or super-user.
1810          */
1811         perm = 0;
1812 +#ifdef CONFIG_GRKERNSEC
1813 +       if (current->tty == tty || capable(CAP_SYS_TTY_CONFIG))
1814 +#else
1815         if (current->tty == tty || suser())
1816 +#endif
1817                 perm = 1;
1818   
1819         kbd = kbd_table + console;
1820 @@ -1038,12 +1043,20 @@
1821                 return do_unimap_ioctl(cmd, (struct unimapdesc *)arg, perm);
1822  
1823         case VT_LOCKSWITCH:
1824 +#ifdef CONFIG_GRKERNSEC
1825 +               if (!capable(CAP_SYS_TTY_CONFIG))
1826 +#else
1827                 if (!suser())
1828 +#endif
1829                    return -EPERM;
1830                 vt_dont_switch = 1;
1831                 return 0;
1832         case VT_UNLOCKSWITCH:
1833 +#ifdef CONFIG_GRKERNSEC
1834 +               if (!capable(CAP_SYS_TTY_CONFIG))
1835 +#else
1836                 if (!suser())
1837 +#endif
1838                    return -EPERM;
1839                 vt_dont_switch = 0;
1840                 return 0;
1841 diff -urN linux-2.4.21/drivers/pci/proc.c linux-2.4.21/drivers/pci/proc.c
1842 --- linux-2.4.21/drivers/pci/proc.c     2003-06-23 11:42:12.000000000 -0400
1843 +++ linux-2.4.21/drivers/pci/proc.c     2003-06-23 11:49:15.000000000 -0400
1844 @@ -562,7 +562,15 @@
1845                 pci_for_each_dev(dev) {
1846                         pci_proc_attach_device(dev);
1847                 }
1848 +#ifdef CONFIG_GRKERNSEC_PROC_ADD
1849 +#ifdef CONFIG_GRKERNSEC_PROC_USER
1850 +               entry = create_proc_entry("pci", S_IRUSR, NULL);
1851 +#elif CONFIG_GRKERNSEC_PROC_USERGROUP
1852 +               entry = create_proc_entry("pci", S_IRUSR | S_IRGRP, NULL);
1853 +#endif
1854 +#else
1855                 entry = create_proc_entry("pci", 0, NULL);
1856 +#endif
1857                 if (entry)
1858                         entry->proc_fops = &proc_pci_operations;
1859         }
1860 diff -urN linux-2.4.21/fs/binfmt_aout.c linux-2.4.21/fs/binfmt_aout.c
1861 --- linux-2.4.21/fs/binfmt_aout.c       2003-06-23 11:41:29.000000000 -0400
1862 +++ linux-2.4.21/fs/binfmt_aout.c       2003-06-23 11:49:16.000000000 -0400
1863 @@ -5,6 +5,7 @@
1864   */
1865  
1866  #include <linux/module.h>
1867 +#include <linux/config.h>
1868  
1869  #include <linux/sched.h>
1870  #include <linux/kernel.h>
1871 @@ -113,10 +114,12 @@
1872  /* If the size of the dump file exceeds the rlimit, then see what would happen
1873     if we wrote the stack, but not the data area.  */
1874  #ifdef __sparc__
1875 +       gr_learn_resource(current, RLIMIT_CORE, dump.u_dsize+dump.u_ssize);
1876         if ((dump.u_dsize+dump.u_ssize) >
1877             current->rlim[RLIMIT_CORE].rlim_cur)
1878                 dump.u_dsize = 0;
1879  #else
1880 +       gr_learn_resource(current, RLIMIT_CORE, (dump.u_dsize+dump.u_ssize+1) * PAGE_SIZE);
1881         if ((dump.u_dsize+dump.u_ssize+1) * PAGE_SIZE >
1882             current->rlim[RLIMIT_CORE].rlim_cur)
1883                 dump.u_dsize = 0;
1884 @@ -124,10 +127,12 @@
1885  
1886  /* Make sure we have enough room to write the stack and data areas. */
1887  #ifdef __sparc__
1888 +       gr_learn_resource(current, RLIMIT_CORE, dump.u_ssize);
1889         if ((dump.u_ssize) >
1890             current->rlim[RLIMIT_CORE].rlim_cur)
1891                 dump.u_ssize = 0;
1892  #else
1893 +       gr_learn_resource(current, RLIMIT_CORE, (dump.u_ssize+1) * PAGE_SIZE);
1894         if ((dump.u_ssize+1) * PAGE_SIZE >
1895             current->rlim[RLIMIT_CORE].rlim_cur)
1896                 dump.u_ssize = 0;
1897 @@ -276,6 +281,8 @@
1898         rlim = current->rlim[RLIMIT_DATA].rlim_cur;
1899         if (rlim >= RLIM_INFINITY)
1900                 rlim = ~0;
1901 +
1902 +       gr_learn_resource(current, RLIMIT_DATA, ex.a_data + ex.a_bss);
1903         if (ex.a_data + ex.a_bss > rlim)
1904                 return -ENOMEM;
1905  
1906 diff -urN linux-2.4.21/fs/binfmt_elf.c linux-2.4.21/fs/binfmt_elf.c
1907 --- linux-2.4.21/fs/binfmt_elf.c        2003-06-23 11:41:29.000000000 -0400
1908 +++ linux-2.4.21/fs/binfmt_elf.c        2003-06-23 11:49:16.000000000 -0400
1909 @@ -11,6 +11,7 @@
1910  
1911  #include <linux/module.h>
1912  
1913 +#include <linux/config.h>
1914  #include <linux/fs.h>
1915  #include <linux/stat.h>
1916  #include <linux/sched.h>
1917 @@ -33,6 +34,7 @@
1918  #include <linux/smp_lock.h>
1919  #include <linux/compiler.h>
1920  #include <linux/highmem.h>
1921 +#include <linux/grsecurity.h>
1922  
1923  #include <asm/uaccess.h>
1924  #include <asm/param.h>
1925 @@ -1003,8 +1163,11 @@
1926  #undef DUMP_SEEK
1927  
1928  #define DUMP_WRITE(addr, nr)   \
1929 +       do { \
1930 +       gr_learn_resource(current, RLIMIT_CORE, size + (nr)); \
1931         if ((size += (nr)) > limit || !dump_write(file, (addr), (nr))) \
1932 -               goto end_coredump;
1933 +               goto end_coredump; \
1934 +       } while (0);
1935  #define DUMP_SEEK(off) \
1936         if (!dump_seek(file, (off))) \
1937                 goto end_coredump;
1938 diff -urN linux-2.4.21/fs/buffer.c linux-2.4.21/fs/buffer.c
1939 --- linux-2.4.21/fs/buffer.c    2003-06-23 11:41:28.000000000 -0400
1940 +++ linux-2.4.21/fs/buffer.c    2003-06-23 11:49:16.000000000 -0400
1941 @@ -1781,6 +1781,9 @@
1942         int err;
1943  
1944         err = -EFBIG;
1945 +
1946 +       gr_learn_resource(current, RLIMIT_FSIZE, (unsigned long) size);
1947 +
1948          limit = current->rlim[RLIMIT_FSIZE].rlim_cur;
1949         if (limit != RLIM_INFINITY && size > (loff_t)limit) {
1950                 send_sig(SIGXFSZ, current, 0);
1951 diff -urN linux-2.4.21/fs/exec.c linux-2.4.21/fs/exec.c
1952 --- linux-2.4.21/fs/exec.c      2003-06-23 11:41:28.000000000 -0400
1953 +++ linux-2.4.21/fs/exec.c      2003-06-23 11:49:16.000000000 -0400
1954 @@ -43,6 +43,8 @@
1955  #include <asm/uaccess.h>
1956  #include <asm/pgalloc.h>
1957  #include <asm/mmu_context.h>
1958 +#include <linux/major.h>
1959 +#include <linux/grsecurity.h>
1960  
1961  #ifdef CONFIG_KMOD
1962  #include <linux/kmod.h>
1963 @@ -687,6 +725,9 @@
1964                         cap_set_full(bprm->cap_effective);
1965         }
1966  
1967 +       if (gr_handle_ptrace_exec(bprm->file->f_dentry, bprm->file->f_vfsmnt))
1968 +               return -EACCES;
1969 +
1970         memset(bprm->buf,0,BINPRM_BUF_SIZE);
1971         return kernel_read(bprm->file,0,bprm->buf,BINPRM_BUF_SIZE);
1972  }
1973 @@ -752,6 +793,8 @@
1974          current->suid = current->euid = current->fsuid = bprm->e_uid;
1975          current->sgid = current->egid = current->fsgid = bprm->e_gid;
1976  
1977 +       gr_handle_chroot_caps(current);
1978 +
1979         if(do_unlock)
1980                 unlock_kernel();
1981         current->keep_capabilities = 0;
1982 @@ -885,6 +928,11 @@
1983         struct file *file;
1984         int retval;
1985         int i;
1986 +#ifdef CONFIG_GRKERNSEC
1987 +       struct file *old_exec_file;
1988 +       struct acl_subject_label *old_acl;
1989 +       struct rlimit old_rlim[RLIM_NLIMITS];
1990 +#endif
1991  
1992         file = open_exec(filename);
1993  
1994 @@ -892,7 +940,21 @@
1995         if (IS_ERR(file))
1996                 return retval;
1997  
1998 +       gr_learn_resource(current, RLIMIT_NPROC, atomic_read(&current->user->processes));
1999 +
2000 +       if (gr_handle_nproc()) {
2001 +               allow_write_access(file);
2002 +               fput(file);
2003 +               return -EAGAIN;
2004 +       }
2005 +
2006 +       if (!gr_acl_handle_execve(file->f_dentry, file->f_vfsmnt)) {
2007 +               allow_write_access(file);
2008 +               fput(file);
2009 +               return -EACCES;
2010 +       }
2011 +
2012         bprm.p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
2013         memset(bprm.page, 0, MAX_ARG_PAGES*sizeof(bprm.page[0])); 
2014  
2015         bprm.file = file;
2016 @@ -916,11 +987,26 @@
2017         if (retval < 0) 
2018                 goto out; 
2019  
2020 +       if (!gr_tpe_allow(file)) {
2021 +               retval = -EACCES;
2022 +               goto out;
2023 +       }
2024 +
2025 +       if(gr_check_crash_exec(file)) {
2026 +               retval = -EACCES;
2027 +               goto out;
2028 +       }
2029 +
2030         retval = copy_strings_kernel(1, &bprm.filename, &bprm);
2031         if (retval < 0) 
2032                 goto out; 
2033  
2034         bprm.exec = bprm.p;
2035 +
2036 +       gr_log_chroot_exec(file->f_dentry, file->f_vfsmnt);
2037 +
2038 +       gr_handle_exec_args(&bprm, argv);
2039 +
2040         retval = copy_strings(bprm.envc, envp, &bprm);
2041         if (retval < 0) 
2042                 goto out; 
2043 @@ -929,11 +1015,32 @@
2044         if (retval < 0) 
2045                 goto out; 
2046  
2047 +#ifdef CONFIG_GRKERNSEC
2048 +       old_acl = current->acl;
2049 +       memcpy(old_rlim, current->rlim, sizeof(old_rlim));
2050 +       old_exec_file = current->exec_file;
2051 +       get_file(file);
2052 +       current->exec_file = file;
2053 +#endif
2054 +
2055 +       gr_set_proc_label(file->f_dentry, file->f_vfsmnt);
2056 +
2057         retval = search_binary_handler(&bprm,regs);
2058 -       if (retval >= 0)
2059 +       if (retval >= 0) {
2060 +#ifdef CONFIG_GRKERNSEC
2061 +               if (old_exec_file)
2062 +                       fput(old_exec_file);
2063 +#endif
2064                 /* execve success */
2065                 return retval;
2066 +       }
2067  
2068 +#ifdef CONFIG_GRKERNSEC
2069 +       current->acl = old_acl;
2070 +       memcpy(current->rlim, old_rlim, sizeof(old_rlim));
2071 +       fput(current->exec_file);
2072 +       current->exec_file = old_exec_file;
2073 +#endif
2074  out:
2075         /* Something went wrong, return the inode and free the argument pages*/
2076         allow_write_access(bprm.file);
2077 @@ -1090,6 +1234,7 @@
2078         if (!is_dumpable(current))
2079                 goto fail;
2080         current->mm->dumpable = 0;
2081 +       gr_learn_resource(current, RLIMIT_CORE, binfmt->min_coredump);
2082         if (current->rlim[RLIMIT_CORE].rlim_cur < binfmt->min_coredump)
2083                 goto fail;
2084  
2085 @@ -1109,7 +1254,7 @@
2086                 goto close_fail;
2087         if (!file->f_op->write)
2088                 goto close_fail;
2089 -       if (do_truncate(file->f_dentry, 0) != 0)
2090 +       if (do_truncate(file->f_dentry, 0, file->f_vfsmnt) != 0)
2091                 goto close_fail;
2092  
2093         retval = binfmt->core_dump(signr, regs, file);
2094 diff -urN linux-2.4.21/fs/fcntl.c linux-2.4.21/fs/fcntl.c
2095 --- linux-2.4.21/fs/fcntl.c     2003-06-23 11:41:29.000000000 -0400
2096 +++ linux-2.4.21/fs/fcntl.c     2003-06-23 11:49:16.000000000 -0400
2097 @@ -11,6 +11,7 @@
2098  #include <linux/smp_lock.h>
2099  #include <linux/slab.h>
2100  #include <linux/iobuf.h>
2101 +#include <linux/grsecurity.h>
2102  
2103  #include <asm/poll.h>
2104  #include <asm/siginfo.h>
2105 @@ -64,6 +65,8 @@
2106         int error;
2107         int start;
2108  
2109 +       gr_learn_resource(current, RLIMIT_NOFILE, orig_start);
2110 +
2111         write_lock(&files->file_lock);
2112         
2113         error = -EINVAL;
2114 @@ -86,6 +89,7 @@
2115         }
2116         
2117         error = -EMFILE;
2118 +       gr_learn_resource(current, RLIMIT_NOFILE, newfd);
2119         if (newfd >= current->rlim[RLIMIT_NOFILE].rlim_cur)
2120                 goto out;
2121  
2122 @@ -141,6 +145,8 @@
2123         struct file * file, *tofree;
2124         struct files_struct * files = current->files;
2125  
2126 +       gr_learn_resource(current, RLIMIT_NOFILE, newfd);
2127 +
2128         write_lock(&files->file_lock);
2129         if (!(file = fcheck(oldfd)))
2130                 goto out_unlock;
2131 @@ -448,6 +454,10 @@
2132                         match = -p->pgrp;
2133                 if (pid != match)
2134                         continue;
2135 +               if (gr_check_protected_task(p))
2136 +                       continue;
2137 +               if (gr_pid_is_chrooted(p))
2138 +                       continue;
2139                 send_sigio_to_task(p, fown, fd, band);
2140         }
2141  out:
2142 diff -urN linux-2.4.21/fs/locks.c linux-2.4.21/fs/locks.c
2143 --- linux-2.4.21/fs/locks.c     2003-06-23 11:41:28.000000000 -0400
2144 +++ linux-2.4.21/fs/locks.c     2003-06-23 11:49:16.000000000 -0400
2145 @@ -138,6 +138,7 @@
2146  static struct file_lock *locks_alloc_lock(int account)
2147  {
2148         struct file_lock *fl;
2149 +       if(account) gr_learn_resource(current, RLIMIT_LOCKS, current->locks);
2150         if (account && current->locks >= current->rlim[RLIMIT_LOCKS].rlim_cur)
2151                 return NULL;
2152         fl = kmem_cache_alloc(filelock_cache, SLAB_KERNEL);
2153 diff -urN linux-2.4.21/fs/namei.c linux-2.4.21/fs/namei.c
2154 --- linux-2.4.21/fs/namei.c     2003-06-23 11:41:28.000000000 -0400
2155 +++ linux-2.4.21/fs/namei.c     2003-06-23 11:49:16.000000000 -0400
2156 @@ -22,6 +22,7 @@
2157  #include <linux/dnotify.h>
2158  #include <linux/smp_lock.h>
2159  #include <linux/personality.h>
2160 +#include <linux/grsecurity.h>
2161  
2162  #include <asm/namei.h>
2163  #include <asm/uaccess.h>
2164 @@ -343,6 +344,13 @@
2165                 current->state = TASK_RUNNING;
2166                 schedule();
2167         }
2168 +
2169 +       if (gr_handle_follow_link(dentry->d_parent->d_inode,
2170 +                                 dentry->d_inode, dentry, nd->mnt)) {
2171 +               path_release(nd);
2172 +               return -EACCES;
2173 +       }
2174 +
2175         current->link_count++;
2176         current->total_link_count++;
2177         UPDATE_ATIME(dentry->d_inode);
2178 @@ -641,6 +649,10 @@
2179                         }
2180                 }
2181  return_base:
2182 +               if (!gr_acl_handle_hidden_file(nd->dentry, nd->mnt)) {
2183 +                       path_release(nd);
2184 +                       return -ENOENT;
2185 +               }
2186                 return 0;
2187  out_dput:
2188                 dput(dentry);
2189 @@ -1013,7 +1025,21 @@
2190                 error = path_lookup(pathname, lookup_flags(flag), nd);
2191                 if (error)
2192                         return error;
2193 +
2194 +               if (gr_acl_is_enabled() && nd->dentry->d_inode && 
2195 +                   S_ISBLK(nd->dentry->d_inode->i_mode) &&
2196 +                   !capable(CAP_SYS_RAWIO)) {
2197 +                       error = -EPERM;
2198 +                       goto exit;
2199 +               }
2200 +       
2201 +               if (!gr_acl_handle_open(nd->dentry, nd->mnt, flag)) {
2202 +                       error = -EACCES;
2203 +                       goto exit;
2204 +               }
2205 +
2206                 dentry = nd->dentry;
2207 +
2208                 goto ok;
2209         }
2210  
2211 @@ -1046,7 +1072,16 @@
2212         if (!dentry->d_inode) {
2213                 if (!IS_POSIXACL(dir->d_inode))
2214                         mode &= ~current->fs->umask;
2215 +               if (!gr_acl_handle_creat(dentry, nd->dentry, nd->mnt, flag, mode)) {
2216 +                       error = -EACCES;
2217 +                       up(&dir->d_inode->i_sem);
2218 +                       goto exit_dput;
2219 +               }
2220 +
2221                 error = vfs_create(dir->d_inode, dentry, mode);
2222 +               if (!error)
2223 +                       gr_handle_create(dentry, nd->mnt);
2224 +
2225                 up(&dir->d_inode->i_sem);
2226                 dput(nd->dentry);
2227                 nd->dentry = dentry;
2228 @@ -1056,12 +1091,35 @@
2229                 /* Don't check for write permission, don't truncate */
2230                 acc_mode = 0;
2231                 flag &= ~O_TRUNC;
2232 +
2233                 goto ok;
2234         }
2235  
2236         /*
2237          * It already exists.
2238          */
2239 +
2240 +       if (gr_acl_is_enabled() && S_ISBLK(dentry->d_inode->i_mode) &&
2241 +           !capable(CAP_SYS_RAWIO)) {
2242 +               error = -EPERM;
2243 +               up(&dir->d_inode->i_sem);
2244 +               goto exit_dput;
2245 +       }
2246 +
2247 +       if (!gr_acl_handle_open(dentry, nd->mnt, flag)) {
2248 +               error = -EACCES;
2249 +               up(&dir->d_inode->i_sem);
2250 +               goto exit_dput;
2251 +       }
2252 +
2253 +       inode = dentry->d_inode;
2254 +
2255 +       if (gr_handle_fifo(dentry, nd->mnt, dir, flag, acc_mode)) {
2256 +               up(&dir->d_inode->i_sem);
2257 +               error = -EACCES;
2258 +               goto exit_dput;
2259 +       }
2260 +
2261         up(&dir->d_inode->i_sem);
2262  
2263         error = -EEXIST;
2264 @@ -1151,7 +1209,7 @@
2265                 if (!error) {
2266                         DQUOT_INIT(inode);
2267                         
2268 -                       error = do_truncate(dentry, 0);
2269 +                       error = do_truncate(dentry,0,nd->mnt);
2270                 }
2271                 put_write_access(inode);
2272                 if (error)
2273 @@ -1182,6 +1240,13 @@
2274          * stored in nd->last.name and we will have to putname() it when we
2275          * are done. Procfs-like symlinks just set LAST_BIND.
2276          */
2277 +
2278 +       if (gr_handle_follow_link(dentry->d_parent->d_inode, dentry->d_inode,
2279 +                                 dentry, nd->mnt)) {
2280 +               error = -EACCES;
2281 +               goto exit_dput;
2282 +       }
2283 +
2284         UPDATE_ATIME(dentry->d_inode);
2285         error = dentry->d_inode->i_op->follow_link(dentry, nd);
2286         dput(dentry);
2287 @@ -1280,6 +1345,18 @@
2288  
2289         mode &= ~current->fs->umask;
2290         if (!IS_ERR(dentry)) {
2291 +               if (gr_handle_chroot_mknod(dentry, nd.mnt, mode)) {
2292 +                       error = -EPERM;
2293 +                       dput(dentry);
2294 +                       goto out_dput;
2295 +               }
2296 +
2297 +               if (!gr_acl_handle_mknod(dentry, nd.dentry, nd.mnt, mode)) {
2298 +                       error = -EACCES;
2299 +                       dput(dentry);
2300 +                       goto out_dput;
2301 +               }
2302 +       
2303                 switch (mode & S_IFMT) {
2304                 case 0: case S_IFREG:
2305                         error = vfs_create(nd.dentry->d_inode,dentry,mode);
2306 @@ -1293,8 +1370,13 @@
2307                 default:
2308                         error = -EINVAL;
2309                 }
2310 +
2311 +               if(!error)
2312 +                       gr_handle_create(dentry, nd.mnt);
2313 +
2314                 dput(dentry);
2315         }
2316 +out_dput:
2317         up(&nd.dentry->d_inode->i_sem);
2318         path_release(&nd);
2319  out:
2320 @@ -1346,7 +1428,16 @@
2321                 if (!IS_ERR(dentry)) {
2322                         if (!IS_POSIXACL(nd.dentry->d_inode))
2323                                 mode &= ~current->fs->umask;
2324 -                       error = vfs_mkdir(nd.dentry->d_inode, dentry, mode);
2325 +                       error = 0;
2326 +
2327 +                       if (!gr_acl_handle_mkdir(dentry, nd.dentry, nd.mnt))
2328 +                               error = -EACCES;
2329 +
2330 +                       if(!error)
2331 +                               error = vfs_mkdir(nd.dentry->d_inode, dentry, mode);
2332 +                       if(!error)
2333 +                               gr_handle_create(dentry, nd.mnt);
2334 +                       
2335                         dput(dentry);
2336                 }
2337                 up(&nd.dentry->d_inode->i_sem);
2338 @@ -1431,6 +1522,8 @@
2339         char * name;
2340         struct dentry *dentry;
2341         struct nameidata nd;
2342 +       ino_t saved_ino = 0;
2343 +       kdev_t saved_dev = 0;
2344  
2345         name = getname(pathname);
2346         if(IS_ERR(name))
2347 @@ -1455,7 +1548,22 @@
2348         dentry = lookup_hash(&nd.last, nd.dentry);
2349         error = PTR_ERR(dentry);
2350         if (!IS_ERR(dentry)) {
2351 -               error = vfs_rmdir(nd.dentry->d_inode, dentry);
2352 +               error = 0;
2353 +               if (dentry->d_inode) {
2354 +                       if (dentry->d_inode->i_nlink <= 1) {
2355 +                               saved_ino = dentry->d_inode->i_ino;
2356 +                               saved_dev = dentry->d_inode->i_dev;
2357 +                       }
2358 +
2359 +                       if (!gr_acl_handle_rmdir(dentry, nd.mnt))
2360 +                               error = -EACCES;
2361 +               }
2362 +
2363 +               if (!error)
2364 +                       error = vfs_rmdir(nd.dentry->d_inode, dentry);
2365 +               if (!error && (saved_dev || saved_ino))
2366 +                       gr_handle_delete(saved_ino,saved_dev);
2367 +
2368                 dput(dentry);
2369         }
2370         up(&nd.dentry->d_inode->i_sem);
2371 @@ -1499,6 +1607,8 @@
2372         char * name;
2373         struct dentry *dentry;
2374         struct nameidata nd;
2375 +       ino_t saved_ino = 0;
2376 +       kdev_t saved_dev = 0;
2377  
2378         name = getname(pathname);
2379         if(IS_ERR(name))
2380 @@ -1517,7 +1627,21 @@
2381                 /* Why not before? Because we want correct error value */
2382                 if (nd.last.name[nd.last.len])
2383                         goto slashes;
2384 -               error = vfs_unlink(nd.dentry->d_inode, dentry);
2385 +               error = 0;
2386 +               if (dentry->d_inode) {
2387 +                       if (dentry->d_inode->i_nlink <= 1) {
2388 +                               saved_ino = dentry->d_inode->i_ino;
2389 +                               saved_dev = dentry->d_inode->i_dev;
2390 +                       }
2391 +
2392 +                       if (!gr_acl_handle_unlink(dentry, nd.mnt))
2393 +                               error = -EACCES;
2394 +               }
2395 +
2396 +               if (!error)
2397 +                       error = vfs_unlink(nd.dentry->d_inode, dentry);
2398 +               if (!error && (saved_ino || saved_dev))
2399 +                       gr_handle_delete(saved_ino,saved_dev);
2400         exit2:
2401                 dput(dentry);
2402         }
2403 @@ -1581,7 +1705,15 @@
2404                 dentry = lookup_create(&nd, 0);
2405                 error = PTR_ERR(dentry);
2406                 if (!IS_ERR(dentry)) {
2407 -                       error = vfs_symlink(nd.dentry->d_inode, dentry, from);
2408 +                       error = 0;
2409 +
2410 +                       if (!gr_acl_handle_symlink(dentry, nd.dentry, nd.mnt, from))
2411 +                               error = -EACCES;
2412 +
2413 +                       if(!error)      
2414 +                               error = vfs_symlink(nd.dentry->d_inode, dentry, from);
2415 +                       if (!error)
2416 +                               gr_handle_create(dentry, nd.mnt);
2417                         dput(dentry);
2418                 }
2419                 up(&nd.dentry->d_inode->i_sem);
2420 @@ -1665,7 +1797,27 @@
2421                 new_dentry = lookup_create(&nd, 0);
2422                 error = PTR_ERR(new_dentry);
2423                 if (!IS_ERR(new_dentry)) {
2424 -                       error = vfs_link(old_nd.dentry, nd.dentry->d_inode, new_dentry);
2425 +                       error = 0;
2426 +
2427 +                       if (gr_handle_hardlink(old_nd.dentry, old_nd.mnt,
2428 +                                              old_nd.dentry->d_inode,
2429 +                                              old_nd.dentry->d_inode->i_mode, to)) {
2430 +                               error = -EPERM;
2431 +                               goto out_error;
2432 +                       }
2433 +
2434 +                       if (!gr_acl_handle_link(new_dentry, nd.dentry, nd.mnt,
2435 +                                                old_nd.dentry, old_nd.mnt, to)) {
2436 +                               error = -EACCES;
2437 +                               goto out_error;
2438 +                       }
2439 +
2440 +                       error = vfs_link(old_nd.dentry, 
2441 +                                       nd.dentry->d_inode, new_dentry);
2442 +
2443 +                       if (!error)
2444 +                               gr_handle_create(new_dentry, nd.mnt);
2445 +out_error:
2446                         dput(new_dentry);
2447                 }
2448                 up(&nd.dentry->d_inode->i_sem);
2449 @@ -1896,10 +2048,15 @@
2450         if (IS_ERR(new_dentry))
2451                 goto exit4;
2452  
2453 -       lock_kernel();
2454 -       error = vfs_rename(old_dir->d_inode, old_dentry,
2455 +       error = gr_acl_handle_rename(new_dentry, newnd.dentry, newnd.mnt,
2456 +                                    old_dentry, old_dir->d_inode, oldnd.mnt, newname);
2457 +
2458 +       if (error == 1) {
2459 +               lock_kernel();
2460 +               error = vfs_rename(old_dir->d_inode, old_dentry,
2461                                    new_dir->d_inode, new_dentry);
2462 -       unlock_kernel();
2463 +               unlock_kernel();
2464 +       }
2465  
2466         dput(new_dentry);
2467  exit4:
2468 diff -urN linux-2.4.21/fs/namespace.c linux-2.4.21/fs/namespace.c
2469 --- linux-2.4.21/fs/namespace.c 2003-06-23 11:41:28.000000000 -0400
2470 +++ linux-2.4.21/fs/namespace.c 2003-06-23 11:49:16.000000000 -0400
2471 @@ -15,6 +15,8 @@
2472  #include <linux/quotaops.h>
2473  #include <linux/acct.h>
2474  #include <linux/module.h>
2475 +#include <linux/sched.h>
2476 +#include <linux/grsecurity.h>
2477  
2478  #include <asm/uaccess.h>
2479  
2480 @@ -325,6 +327,8 @@
2481                         lock_kernel();
2482                         retval = do_remount_sb(sb, MS_RDONLY, 0);
2483                         unlock_kernel();
2484 +
2485 +                       gr_log_remount(mnt->mnt_devname, retval);
2486                 }
2487                 up_write(&sb->s_umount);
2488                 return retval;
2489 @@ -350,6 +354,9 @@
2490         }
2491         spin_unlock(&dcache_lock);
2492         up_write(&current->namespace->sem);
2493 +
2494 +       gr_log_unmount(mnt->mnt_devname, retval);
2495 +
2496         return retval;
2497  }
2498  
2499 @@ -729,6 +736,12 @@
2500         if (retval)
2501                 return retval;
2502  
2503 +       if (gr_handle_chroot_mount(nd.dentry, nd.mnt, dev_name)) {
2504 +               retval = -EPERM;
2505 +               path_release(&nd);
2506 +               return retval;
2507 +       }
2508 +
2509         if (flags & MS_REMOUNT)
2510                 retval = do_remount(&nd, flags & ~MS_REMOUNT, mnt_flags,
2511                                     data_page);
2512 @@ -740,6 +753,9 @@
2513                 retval = do_add_mount(&nd, type_page, flags, mnt_flags,
2514                                       dev_name, data_page);
2515         path_release(&nd);
2516 +
2517 +       gr_log_mount(dev_name, dir_name, retval);
2518 +
2519         return retval;
2520  }
2521  
2522 @@ -909,6 +925,9 @@
2523         if (!capable(CAP_SYS_ADMIN))
2524                 return -EPERM;
2525  
2526 +       if (gr_handle_chroot_pivot())
2527 +               return -EPERM;
2528 +
2529         lock_kernel();
2530  
2531         error = __user_walk(new_root, LOOKUP_POSITIVE|LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &new_nd);
2532 diff -urN linux-2.4.21/fs/open.c linux-2.4.21/fs/open.c
2533 --- linux-2.4.21/fs/open.c      2003-06-23 11:41:28.000000000 -0400
2534 +++ linux-2.4.21/fs/open.c      2003-06-23 11:49:16.000000000 -0400
2535 @@ -15,6 +15,7 @@
2536  #include <linux/slab.h>
2537  #include <linux/tty.h>
2538  #include <linux/iobuf.h>
2539 +#include <linux/grsecurity.h>
2540  
2541  #include <asm/uaccess.h>
2542  
2543 @@ -95,7 +96,7 @@
2544         write_unlock(&files->file_lock);
2545  }
2546  
2547 -int do_truncate(struct dentry *dentry, loff_t length)
2548 +int do_truncate(struct dentry *dentry, loff_t length, struct vfsmount *mnt)
2549  {
2550         struct inode *inode = dentry->d_inode;
2551         int error;
2552 @@ -105,6 +106,9 @@
2553         if (length < 0)
2554                 return -EINVAL;
2555  
2556 +       if (!gr_acl_handle_truncate(dentry, mnt))
2557 +               return -EACCES;
2558 +
2559         down(&inode->i_sem);
2560         newattrs.ia_size = length;
2561         newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
2562 @@ -163,7 +167,7 @@
2563         error = locks_verify_truncate(inode, NULL, length);
2564         if (!error) {
2565                 DQUOT_INIT(inode);
2566 -               error = do_truncate(nd.dentry, length);
2567 +               error = do_truncate(nd.dentry, length, nd.mnt);
2568         }
2569         put_write_access(inode);
2570  
2571 @@ -215,7 +219,7 @@
2572  
2573         error = locks_verify_truncate(inode, file, length);
2574         if (!error)
2575 -               error = do_truncate(dentry, length);
2576 +               error = do_truncate(dentry, length, file->f_vfsmnt);
2577  out_putf:
2578         fput(file);
2579  out:
2580 @@ -284,6 +288,12 @@
2581                     (error = permission(inode,MAY_WRITE)) != 0)
2582                         goto dput_and_out;
2583         }
2584 +
2585 +       if (!gr_acl_handle_utime(nd.dentry, nd.mnt)) {
2586 +               error = -EACCES;
2587 +               goto dput_and_out;
2588 +       }
2589 +
2590         error = notify_change(nd.dentry, &newattrs);
2591  dput_and_out:
2592         path_release(&nd);
2593 @@ -329,6 +339,12 @@
2594                     (error = permission(inode,MAY_WRITE)) != 0)
2595                         goto dput_and_out;
2596         }
2597 +
2598 +       if (!gr_acl_handle_utime(nd.dentry, nd.mnt)) {
2599 +               error = -EACCES;
2600 +               goto dput_and_out;
2601 +       }
2602 +
2603         error = notify_change(nd.dentry, &newattrs);
2604  dput_and_out:
2605         path_release(&nd);
2606 @@ -371,6 +387,10 @@
2607                 if(!res && (mode & S_IWOTH) && IS_RDONLY(nd.dentry->d_inode)
2608                    && !special_file(nd.dentry->d_inode->i_mode))
2609                         res = -EROFS;
2610 +               
2611 +               if (!res && !gr_acl_handle_access(nd.dentry, nd.mnt, mode))
2612 +                       res =  -EACCES;
2613 +
2614                 path_release(&nd);
2615         }
2616  
2617 @@ -394,6 +414,8 @@
2618         if (error)
2619                 goto dput_and_out;
2620  
2621 +       gr_log_chdir(nd.dentry, nd.mnt);
2622 +
2623         set_fs_pwd(current->fs, nd.mnt, nd.dentry);
2624  
2625  dput_and_out:
2626 @@ -424,6 +446,13 @@
2627                 goto out_putf;
2628  
2629         error = permission(inode, MAY_EXEC);
2630 +
2631 +       if (!error && !gr_chroot_fchdir(dentry, mnt))
2632 +               error = -EPERM;
2633 +
2634 +       if (!error)
2635 +               gr_log_chdir(dentry, mnt);
2636 +
2637         if (!error)
2638                 set_fs_pwd(current->fs, mnt, dentry);
2639  out_putf:
2640 @@ -450,8 +479,16 @@
2641         if (!capable(CAP_SYS_CHROOT))
2642                 goto dput_and_out;
2643  
2644 +       if (gr_handle_chroot_chroot(nd.dentry, nd.mnt))
2645 +               goto dput_and_out;
2646 +
2647         set_fs_root(current->fs, nd.mnt, nd.dentry);
2648         set_fs_altroot();
2649 +
2650 +       gr_handle_chroot_caps(current);
2651 +
2652 +       gr_handle_chroot_chdir(nd.dentry, nd.mnt);
2653 +
2654         error = 0;
2655  dput_and_out:
2656         path_release(&nd);
2657 @@ -480,8 +517,20 @@
2658         err = -EPERM;
2659         if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
2660                 goto out_putf;
2661 +
2662 +       if (!gr_acl_handle_fchmod(dentry, file->f_vfsmnt, mode)) {
2663 +               err = -EACCES;
2664 +               goto out_putf;
2665 +       }
2666 +
2667         if (mode == (mode_t) -1)
2668                 mode = inode->i_mode;
2669 +
2670 +       if (gr_handle_chroot_chmod(dentry, file->f_vfsmnt, mode)) {
2671 +               err = -EPERM;
2672 +               goto out_putf;
2673 +       }           
2674 +
2675         newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
2676         newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
2677         err = notify_change(dentry, &newattrs);
2678 @@ -512,8 +561,19 @@
2679         if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
2680                 goto dput_and_out;
2681  
2682 +       if (!gr_acl_handle_chmod(nd.dentry, nd.mnt, mode)) {
2683 +               error = -EACCES;
2684 +               goto dput_and_out;
2685 +       }
2686 +
2687         if (mode == (mode_t) -1)
2688                 mode = inode->i_mode;
2689 +
2690 +       if (gr_handle_chroot_chmod(nd.dentry, nd.mnt, mode)) {
2691 +               error = -EACCES;
2692 +               goto dput_and_out;
2693 +       }
2694 +
2695         newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
2696         newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
2697         error = notify_change(nd.dentry, &newattrs);
2698 @@ -524,7 +584,7 @@
2699         return error;
2700  }
2701  
2702 -static int chown_common(struct dentry * dentry, uid_t user, gid_t group)
2703 +static int chown_common(struct dentry * dentry, uid_t user, gid_t group, struct vfsmount *mnt)
2704  {
2705         struct inode * inode;
2706         int error;
2707 @@ -541,6 +601,12 @@
2708         error = -EPERM;
2709         if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
2710                 goto out;
2711 +
2712 +       if (!gr_acl_handle_chown(dentry, mnt)) {
2713 +               error = -EACCES;
2714 +               goto out;
2715 +       }
2716 +
2717         if (user == (uid_t) -1)
2718                 user = inode->i_uid;
2719         if (group == (gid_t) -1)
2720 @@ -591,7 +657,7 @@
2721  
2722         error = user_path_walk(filename, &nd);
2723         if (!error) {
2724 -               error = chown_common(nd.dentry, user, group);
2725 +               error = chown_common(nd.dentry, user, group, nd.mnt);
2726                 path_release(&nd);
2727         }
2728         return error;
2729 @@ -604,7 +670,7 @@
2730  
2731         error = user_path_walk_link(filename, &nd);
2732         if (!error) {
2733 -               error = chown_common(nd.dentry, user, group);
2734 +               error = chown_common(nd.dentry, user, group, nd.mnt);
2735                 path_release(&nd);
2736         }
2737         return error;
2738 @@ -618,7 +684,8 @@
2739  
2740         file = fget(fd);
2741         if (file) {
2742 -               error = chown_common(file->f_dentry, user, group);
2743 +               error = chown_common(file->f_dentry, user,
2744 +                               group, file->f_vfsmnt);
2745                 fput(file);
2746         }
2747         return error;
2748 @@ -738,6 +805,7 @@
2749          * N.B. For clone tasks sharing a files structure, this test
2750          * will limit the total number of files that can be opened.
2751          */
2752 +       gr_learn_resource(current, RLIMIT_NOFILE, fd);
2753         if (fd >= current->rlim[RLIMIT_NOFILE].rlim_cur)
2754                 goto out;
2755  
2756 diff -urN linux-2.4.21/fs/proc/array.c linux-2.4.21/fs/proc/array.c
2757 --- linux-2.4.21/fs/proc/array.c        2003-06-23 11:41:29.000000000 -0400
2758 +++ linux-2.4.21/fs/proc/array.c        2003-06-23 11:49:16.000000000 -0400
2759 @@ -334,6 +340,12 @@
2760  
2761         wchan = get_wchan(task);
2762  
2763 +#ifdef CONFIG_GRKERNSEC_HIDESYM
2764 +       wchan = 0;
2765 +       eip = 0;
2766 +       esp = 0;
2767 +#endif
2768 +
2769         collect_sigign_sigcatch(task, &sigign, &sigcatch);
2770  
2771         /* scale priority and nice values from timeslices to -20..20 */
2772 @@ -684,6 +727,16 @@
2773         return retval;
2774  }
2775  
2776 +#ifdef CONFIG_GRKERNSEC_PROC_IPADDR     
2777 +int proc_pid_ipaddr(struct task_struct *task, char * buffer)    
2778 +{       
2779 +       int len;         
2780 +
2781 +       len = sprintf(buffer, "%u.%u.%u.%u\n", NIPQUAD(task->curr_ip));  
2782 +       return len;      
2783 +}       
2784 +#endif
2785 +
2786  #ifdef CONFIG_SMP
2787  int proc_pid_cpu(struct task_struct *task, char * buffer)
2788  {
2789 diff -urN linux-2.4.21/fs/proc/base.c linux-2.4.21/fs/proc/base.c
2790 --- linux-2.4.21/fs/proc/base.c 2003-06-23 11:41:29.000000000 -0400
2791 +++ linux-2.4.21/fs/proc/base.c 2003-06-23 11:49:16.000000000 -0400
2792 @@ -25,6 +25,7 @@
2793  #include <linux/string.h>
2794  #include <linux/seq_file.h>
2795  #include <linux/namespace.h>
2796 +#include <linux/grsecurity.h>
2797  
2798  /*
2799   * For hysterical raisins we keep the same inumbers as in the old procfs.
2800 @@ -41,6 +42,9 @@
2801  int proc_pid_status(struct task_struct*,char*);
2802  int proc_pid_statm(struct task_struct*,char*);
2803  int proc_pid_cpu(struct task_struct*,char*);
2804 +#ifdef CONFIG_GRKERNSEC_PROC_IPADDR
2805 +int proc_pid_ipaddr(struct task_struct*,char*);
2806 +#endif
2807  
2808  static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
2809  {
2810 @@ -228,9 +232,22 @@
2811  
2812  static int proc_permission(struct inode *inode, int mask)
2813  {
2814 +       int ret;
2815 +       struct task_struct *task;
2816 +
2817         if (vfs_permission(inode, mask) != 0)
2818                 return -EACCES;
2819 -       return proc_check_root(inode);
2820 +       ret = proc_check_root(inode);
2821 +
2822 +       if (ret)
2823 +               return ret;
2824 +
2825 +       task = inode->u.proc_i.task;
2826 +
2827 +       if (!task)
2828 +               return 0;
2829 +
2830 +       return gr_acl_handle_procpidmem(task);
2831  }
2832  
2833  static ssize_t pid_maps_read(struct file * file, char * buf,
2834 @@ -546,6 +563,9 @@
2835         PROC_PID_STATM,
2836         PROC_PID_MAPS,
2837         PROC_PID_CPU,
2838 +#ifdef CONFIG_GRKERNSEC_PROC_IPADDR
2839 +       PROC_PID_IPADDR,
2840 +#endif
2841         PROC_PID_MOUNTS,
2842         PROC_PID_FD_DIR = 0x8000,       /* 0x8000-0xffff */
2843  };
2844 @@ -561,6 +581,9 @@
2845  #ifdef CONFIG_SMP
2846    E(PROC_PID_CPU,      "cpu",          S_IFREG|S_IRUGO),
2847  #endif
2848 +#ifdef CONFIG_GRKERNSEC_PROC_IPADDR
2849 +  E(PROC_PID_IPADDR,   "ipaddr",       S_IFREG|S_IRUSR),
2850 +#endif
2851    E(PROC_PID_MAPS,     "maps",         S_IFREG|S_IRUGO),
2852    E(PROC_PID_MEM,      "mem",          S_IFREG|S_IRUSR|S_IWUSR),
2853    E(PROC_PID_CWD,      "cwd",          S_IFLNK|S_IRWXUGO),
2854 @@ -717,10 +740,17 @@
2855         get_task_struct(task);
2856         inode->u.proc_i.task = task;
2857         inode->i_uid = 0;
2858 +#ifdef CONFIG_GRKERNSEC_PROC_USERGROUP
2859 +       inode->i_gid = CONFIG_GRKERNSEC_PROC_GID;
2860 +#else
2861         inode->i_gid = 0;
2862 +#endif
2863 +
2864         if (ino == PROC_PID_INO || task_dumpable(task)) {
2865                 inode->i_uid = task->euid;
2866 +#ifndef CONFIG_GRKERNSEC_PROC_USERGROUP
2867                 inode->i_gid = task->egid;
2868 +#endif
2869         }
2870  
2871  out:
2872 @@ -928,6 +958,12 @@
2873                         inode->u.proc_i.op.proc_read = proc_pid_cpu;
2874                         break;
2875  #endif
2876 +#ifdef CONFIG_GRKERNSEC_PROC_IPADDR
2877 +               case PROC_PID_IPADDR:
2878 +                       inode->i_fop = &proc_info_file_operations;
2879 +                       inode->u.proc_i.op.proc_read = proc_pid_ipaddr;
2880 +                       break;
2881 +#endif
2882                 case PROC_PID_MEM:
2883                         inode->i_op = &proc_mem_inode_operations;
2884                         inode->i_fop = &proc_mem_operations;
2885 @@ -1026,13 +1062,34 @@
2886         if (!task)
2887                 goto out;
2888  
2889 +       if(gr_check_hidden_task(task)) {
2890 +               free_task_struct(task);
2891 +               goto out;
2892 +       }
2893 +       
2894 +#ifdef CONFIG_GRKERNSEC_PROC
2895 +       if (current->uid && (task->uid != current->uid)
2896 +#ifdef CONFIG_GRKERNSEC_PROC_USERGROUP
2897 +           && !in_group_p(CONFIG_GRKERNSEC_PROC_GID)
2898 +#endif
2899 +       ) {
2900 +               free_task_struct(task);
2901 +               goto out;
2902 +       }       
2903 +#endif
2904         inode = proc_pid_make_inode(dir->i_sb, task, PROC_PID_INO);
2905  
2906         free_task_struct(task);
2907  
2908         if (!inode)
2909                 goto out;
2910 +#ifdef CONFIG_GRKERNSEC_PROC_USER
2911 +       inode->i_mode = S_IFDIR|S_IRUSR|S_IXUSR;
2912 +#elif CONFIG_GRKERNSEC_PROC_USERGROUP
2913 +       inode->i_mode = S_IFDIR|S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP;
2914 +#else
2915         inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2916 +#endif
2917         inode->i_op = &proc_base_inode_operations;
2918         inode->i_fop = &proc_base_operations;
2919         inode->i_nlink = 3;
2920 @@ -1072,6 +1129,18 @@
2921                 int pid = p->pid;
2922                 if (!pid)
2923                         continue;
2924 +               if(gr_pid_is_chrooted(p))
2925 +                       continue;
2926 +               if(gr_check_hidden_task(p)) 
2927 +                       continue;
2928 +#ifdef CONFIG_GRKERNSEC_PROC
2929 +               if (current->uid && (p->uid != current->uid)
2930 +#ifdef CONFIG_GRKERNSEC_PROC_USERGROUP
2931 +                       && !in_group_p(CONFIG_GRKERNSEC_PROC_GID)
2932 +#endif
2933 +               )
2934 +                       continue;       
2935 +#endif
2936                 if (--index >= 0)
2937                         continue;
2938                 pids[nr_pids] = pid;
2939 diff -urN linux-2.4.21/fs/proc/generic.c linux-2.4.21/fs/proc/generic.c
2940 --- linux-2.4.21/fs/proc/generic.c      2003-07-29 17:57:01.000000000 -0400
2941 +++ linux-2.4.21/fs/proc/generic.c      2003-07-29 17:58:19.000000000 -0400
2942 @@ -503,6 +503,32 @@
2943         return ent;
2944  }
2945  
2946 +#ifdef CONFIG_GRKERNSEC_PROC
2947 +struct proc_dir_entry *proc_priv_mkdir(const char *name, struct proc_dir_entry *parent)
2948 +{
2949 +       struct proc_dir_entry *ent;
2950 +       mode_t mode = 0;
2951 +
2952 +#ifdef CONFIG_GRKERNSEC_PROC_USER
2953 +       mode = S_IFDIR | S_IRUSR | S_IXUSR;
2954 +#elif CONFIG_GRKERNSEC_PROC_USERGROUP
2955 +       mode = S_IFDIR | S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP;
2956 +#endif
2957 +
2958 +       ent = proc_create(&parent, name, mode, 2);
2959 +       if (ent) {
2960 +               ent->proc_fops = &proc_dir_operations;
2961 +               ent->proc_iops = &proc_dir_inode_operations;
2962 +
2963 +               if (proc_register(parent, ent) < 0) {
2964 +                       kfree(ent);
2965 +                       ent = NULL;
2966 +               }
2967 +       }
2968 +       return ent;
2969 +}
2970 +#endif
2971 +
2972  struct proc_dir_entry *create_proc_entry(const char *name, mode_t mode,
2973                                          struct proc_dir_entry *parent)
2974  {
2975 diff -urN linux-2.4.21/fs/proc/inode.c linux-2.4.21/fs/proc/inode.c
2976 --- linux-2.4.21/fs/proc/inode.c        2003-06-23 11:41:29.000000000 -0400
2977 +++ linux-2.4.21/fs/proc/inode.c        2003-06-23 11:49:16.000000000 -0400
2978 @@ -152,7 +152,11 @@
2979                 if (de->mode) {
2980                         inode->i_mode = de->mode;
2981                         inode->i_uid = de->uid;
2982 +#ifdef CONFIG_GRKERNSEC_PROC_USERGROUP
2983 +                       inode->i_gid = CONFIG_GRKERNSEC_PROC_GID;
2984 +#else
2985                         inode->i_gid = de->gid;
2986 +#endif                 
2987                 }
2988                 if (de->size)
2989                         inode->i_size = de->size;
2990 diff -urN linux-2.4.21/fs/proc/proc_misc.c linux-2.4.21/fs/proc/proc_misc.c
2991 --- linux-2.4.21/fs/proc/proc_misc.c    2003-06-23 11:41:29.000000000 -0400
2992 +++ linux-2.4.21/fs/proc/proc_misc.c    2003-06-23 11:49:16.000000000 -0400
2993 @@ -568,6 +568,7 @@
2994  void __init proc_misc_init(void)
2995  {
2996         struct proc_dir_entry *entry;
2997 +       int gr_mode = 0;
2998         static struct {
2999                 char *name;
3000                 int (*read_proc)(char*,char**,off_t,int,int*,void*);
3001 @@ -582,48 +583,81 @@
3002  #ifdef CONFIG_STRAM_PROC
3003                 {"stram",       stram_read_proc},
3004  #endif
3005 -#ifdef CONFIG_MODULES
3006 +#if defined(CONFIG_MODULES) && !defined(CONFIG_GRKERNSEC_PROC)
3007                 {"modules",     modules_read_proc},
3008  #endif
3009                 {"stat",        kstat_read_proc},
3010 +#ifndef CONFIG_GRKERNSEC_PROC_ADD
3011                 {"devices",     devices_read_proc},
3012 -#if !defined(CONFIG_ARCH_S390)
3013 +#endif
3014 +#if !defined(CONFIG_ARCH_S390) && !defined(CONFIG_GRKERNSEC_PROC_ADD)
3015                 {"interrupts",  interrupts_read_proc},
3016  #endif
3017                 {"filesystems", filesystems_read_proc},
3018 +#ifndef CONFIG_GRKERNSEC_PROC_ADD
3019                 {"dma",         dma_read_proc},
3020                 {"ioports",     ioports_read_proc},
3021                 {"cmdline",     cmdline_read_proc},
3022 +#endif
3023  #ifdef CONFIG_SGI_DS1286
3024                 {"rtc",         ds1286_read_proc},
3025  #endif
3026                 {"locks",       locks_read_proc},
3027                 {"swaps",       swaps_read_proc},
3028 +#ifndef CONFIG_GRKERNSEC_PROC_ADD
3029                 {"iomem",       memory_read_proc},
3030 +#endif
3031                 {"execdomains", execdomains_read_proc},
3032                 {NULL,}
3033         };
3034         for (p = simple_ones; p->name; p++)
3035                 create_proc_read_entry(p->name, 0, NULL, p->read_proc, NULL);
3036  
3037 +#ifdef CONFIG_GRKERNSEC_PROC_USER
3038 +       gr_mode = S_IRUSR;
3039 +#elif CONFIG_GRKERNSEC_PROC_USERGROUP
3040 +       gr_mode = S_IRUSR | S_IRGRP;
3041 +#endif
3042 +
3043 +#if defined(CONFIG_GRKERNSEC_PROC) && defined(CONFIG_MODULES)
3044 +       create_proc_read_entry("modules", gr_mode, NULL, &modules_read_proc, NULL);
3045 +#endif
3046 +#ifdef CONFIG_GRKERNSEC_PROC_ADD
3047 +       create_proc_read_entry("devices", gr_mode, NULL, &devices_read_proc, NULL);
3048 +       create_proc_read_entry("dma", gr_mode, NULL, &dma_read_proc, NULL);
3049 +       create_proc_read_entry("ioports", gr_mode, NULL, &ioports_read_proc, NULL);
3050 +       create_proc_read_entry("cmdline", gr_mode, NULL, &cmdline_read_proc, NULL);
3051 +       create_proc_read_entry("iomem", gr_mode, NULL, &memory_read_proc, NULL);
3052 +#if !defined(CONFIG_ARCH_S390)
3053 +       create_proc_read_entry("interrupts", gr_mode, NULL, &interrupts_read_proc, NULL);
3054 +#endif
3055 +#endif
3056 +
3057         proc_symlink("mounts", NULL, "self/mounts");
3058  
3059         /* And now for trickier ones */
3060         entry = create_proc_entry("kmsg", S_IRUSR, &proc_root);
3061         if (entry)
3062                 entry->proc_fops = &proc_kmsg_operations;
3063 +#ifdef CONFIG_GRKERNSEC_PROC_ADD
3064 +       create_seq_entry("cpuinfo", gr_mode, &proc_cpuinfo_operations);
3065 +       create_seq_entry("slabinfo", gr_mode,&proc_slabinfo_operations);
3066 +#else
3067         create_seq_entry("cpuinfo", 0, &proc_cpuinfo_operations);
3068 -       create_seq_entry("partitions", 0, &proc_partitions_operations);
3069         create_seq_entry("slabinfo",S_IWUSR|S_IRUGO,&proc_slabinfo_operations);
3070 +#endif
3071 +       create_seq_entry("partitions", 0, &proc_partitions_operations);
3072  #ifdef CONFIG_MODULES
3073 -       create_seq_entry("ksyms", 0, &proc_ksyms_operations);
3074 +       create_seq_entry("ksyms", gr_mode, &proc_ksyms_operations);
3075  #endif
3076 +#ifndef CONFIG_GRKERNSEC_PROC_ADD
3077         proc_root_kcore = create_proc_entry("kcore", S_IRUSR, NULL);
3078         if (proc_root_kcore) {
3079                 proc_root_kcore->proc_fops = &proc_kcore_operations;
3080                 proc_root_kcore->size =
3081                                 (size_t)high_memory - PAGE_OFFSET + PAGE_SIZE;
3082         }
3083 +#endif
3084         if (prof_shift) {
3085                 entry = create_proc_entry("profile", S_IWUSR | S_IRUGO, NULL);
3086                 if (entry) {
3087 diff -urN linux-2.4.21/fs/proc/proc_tty.c linux-2.4.21/fs/proc/proc_tty.c
3088 --- linux-2.4.21/fs/proc/proc_tty.c     2003-06-23 11:41:29.000000000 -0400
3089 +++ linux-2.4.21/fs/proc/proc_tty.c     2003-06-23 11:49:16.000000000 -0400
3090 @@ -174,7 +174,11 @@
3091          * password lengths and inter-keystroke timings during password
3092          * entry.
3093          */
3094 +#ifdef CONFIG_GRKERNSEC_PROC
3095 +       proc_tty_driver = proc_priv_mkdir("tty/driver", 0);
3096 +#else
3097         proc_tty_driver = proc_mkdir_mode("tty/driver", S_IRUSR | S_IXUSR, 0);
3098 +#endif
3099  
3100         create_proc_read_entry("tty/ldiscs", 0, 0, tty_ldiscs_read_proc,NULL);
3101         create_proc_read_entry("tty/drivers", 0, 0, tty_drivers_read_proc,NULL);
3102 diff -urN linux-2.4.21/fs/proc/root.c linux-2.4.21/fs/proc/root.c
3103 --- linux-2.4.21/fs/proc/root.c 2003-06-23 11:41:29.000000000 -0400
3104 +++ linux-2.4.21/fs/proc/root.c 2003-06-23 11:49:16.000000000 -0400
3105 @@ -37,13 +37,21 @@
3106                 return;
3107         }
3108         proc_misc_init();
3109 +#ifdef CONFIG_GRKERNSEC_PROC
3110 +       proc_net = proc_priv_mkdir("net", 0);
3111 +#else
3112         proc_net = proc_mkdir("net", 0);
3113 +#endif
3114  #ifdef CONFIG_SYSVIPC
3115         proc_mkdir("sysvipc", 0);
3116  #endif
3117  #ifdef CONFIG_SYSCTL
3118 +#ifdef CONFIG_GRKERNSEC_PROC
3119 +       proc_sys_root = proc_priv_mkdir("sys", 0);
3120 +#else
3121         proc_sys_root = proc_mkdir("sys", 0);
3122  #endif
3123 +#endif
3124  #if defined(CONFIG_BINFMT_MISC) || defined(CONFIG_BINFMT_MISC_MODULE)
3125         proc_mkdir("sys/fs", 0);
3126         proc_mkdir("sys/fs/binfmt_misc", 0);
3127 @@ -67,7 +75,12 @@
3128  #ifdef CONFIG_PPC_RTAS
3129         proc_rtas_init();
3130  #endif
3131 +
3132 +#ifdef CONFIG_GRKERNSEC_PROC_ADD
3133 +       proc_bus = proc_priv_mkdir("bus", 0);
3134 +#else
3135         proc_bus = proc_mkdir("bus", 0);
3136 +#endif
3137  }
3138  
3139  static struct dentry *proc_root_lookup(struct inode * dir, struct dentry * dentry)
3140 diff -urN linux-2.4.21/fs/readdir.c linux-2.4.21/fs/readdir.c
3141 --- linux-2.4.21/fs/readdir.c   2003-06-23 11:41:29.000000000 -0400
3142 +++ linux-2.4.21/fs/readdir.c   2003-06-23 11:49:16.000000000 -0400
3143 @@ -10,6 +10,7 @@
3144  #include <linux/stat.h>
3145  #include <linux/file.h>
3146  #include <linux/smp_lock.h>
3147 +#include <linux/grsecurity.h>
3148  
3149  #include <asm/uaccess.h>
3150  
3151 @@ -181,6 +182,7 @@
3152  struct readdir_callback {
3153         struct old_linux_dirent * dirent;
3154         int count;
3155 +       struct nameidata nd;
3156  };
3157  
3158  static int fillonedir(void * __buf, const char * name, int namlen, loff_t offset,
3159 @@ -191,6 +193,10 @@
3160  
3161         if (buf->count)
3162                 return -EINVAL;
3163 +
3164 +       if (!gr_acl_handle_filldir(buf->nd.dentry, buf->nd.mnt, ino))
3165 +               return 0;
3166 +           
3167         buf->count++;
3168         dirent = buf->dirent;
3169         put_user(ino, &dirent->d_ino);
3170 @@ -215,6 +221,9 @@
3171         buf.count = 0;
3172         buf.dirent = dirent;
3173  
3174 +       buf.nd.dentry = file->f_dentry;
3175 +       buf.nd.mnt = file->f_vfsmnt;
3176 +
3177         error = vfs_readdir(file, fillonedir, &buf);
3178         if (error >= 0)
3179                 error = buf.count;
3180 @@ -242,6 +251,7 @@
3181         struct linux_dirent * previous;
3182         int count;
3183         int error;
3184 +       struct nameidata nd;
3185  };
3186  
3187  static int filldir(void * __buf, const char * name, int namlen, loff_t offset,
3188 @@ -254,6 +264,10 @@
3189         buf->error = -EINVAL;   /* only used if we fail.. */
3190         if (reclen > buf->count)
3191                 return -EINVAL;
3192 +
3193 +       if (!gr_acl_handle_filldir(buf->nd.dentry, buf->nd.mnt, ino))
3194 +               return 0;
3195 +
3196         dirent = buf->previous;
3197         if (dirent)
3198                 put_user(offset, &dirent->d_off);
3199 @@ -286,6 +300,9 @@
3200         buf.count = count;
3201         buf.error = 0;
3202  
3203 +       buf.nd.dentry = file->f_dentry;
3204 +       buf.nd.mnt = file->f_vfsmnt;
3205 +
3206         error = vfs_readdir(file, filldir, &buf);
3207         if (error < 0)
3208                 goto out_putf;
3209 @@ -320,6 +337,7 @@
3210         struct linux_dirent64 * previous;
3211         int count;
3212         int error;
3213 +       struct nameidata nd;
3214  };
3215  
3216  static int filldir64(void * __buf, const char * name, int namlen, loff_t offset,
3217 @@ -332,6 +350,10 @@
3218         buf->error = -EINVAL;   /* only used if we fail.. */
3219         if (reclen > buf->count)
3220                 return -EINVAL;
3221 +
3222 +       if (!gr_acl_handle_filldir(buf->nd.dentry, buf->nd.mnt, ino))
3223 +               return 0;
3224 +       
3225         dirent = buf->previous;
3226         if (dirent) {
3227                 d.d_off = offset;
3228 @@ -369,6 +391,9 @@
3229         buf.count = count;
3230         buf.error = 0;
3231  
3232 +       buf.nd.mnt = file->f_vfsmnt;
3233 +       buf.nd.dentry = file->f_dentry;
3234 +
3235         error = vfs_readdir(file, filldir64, &buf);
3236         if (error < 0)
3237                 goto out_putf;
3238 diff -urN linux-2.4.21/grsecurity/Config.in linux-2.4.21/grsecurity/Config.in
3239 --- linux-2.4.21/grsecurity/Config.in   1969-12-31 19:00:00.000000000 -0500
3240 +++ linux-2.4.21/grsecurity/Config.in   2003-07-29 17:58:19.000000000 -0400
3241 @@ -0,0 +1,260 @@
3242 +choice 'Security level' \
3243 +        "Low           CONFIG_GRKERNSEC_LOW \
3244 +         Medium                CONFIG_GRKERNSEC_MID \
3245 +         High          CONFIG_GRKERNSEC_HI \
3246 +         Customized    CONFIG_GRKERNSEC_CUSTOM" Customized
3247 +if [ "$CONFIG_GRKERNSEC_LOW" = "y" ]; then
3248 +define_bool CONFIG_GRKERNSEC_RANDSRC n
3249 +define_bool CONFIG_GRKERNSEC_RANDRPC n
3250 +define_bool CONFIG_GRKERNSEC_FORKFAIL n
3251 +define_bool CONFIG_GRKERNSEC_TIME n
3252 +define_bool CONFIG_GRKERNSEC_SIGNAL n
3253 +define_bool CONFIG_GRKERNSEC_CHROOT_SHMAT n
3254 +define_bool CONFIG_GRKERNSEC_CHROOT_MOUNT n
3255 +define_bool CONFIG_GRKERNSEC_CHROOT_FCHDIR n
3256 +define_bool CONFIG_GRKERNSEC_CHROOT_DOUBLE n
3257 +define_bool CONFIG_GRKERNSEC_CHROOT_PIVOT n
3258 +define_bool CONFIG_GRKERNSEC_CHROOT_MKNOD n
3259 +define_bool CONFIG_GRKERNSEC_PROC n
3260 +define_bool CONFIG_GRKERNSEC_PROC_IPADDR n
3261 +define_bool CONFIG_GRKERNSEC_HIDESYM n
3262 +define_bool CONFIG_GRKERNSEC_CHROOT_CAPS n
3263 +define_bool CONFIG_GRKERNSEC_CHROOT_SYSCTL n
3264 +define_bool CONFIG_GRKERNSEC_PROC_USERGROUP n
3265 +define_bool CONFIG_GRKERNSEC_KMEM n
3266 +define_bool CONFIG_GRKERNSEC_PROC_ADD n
3267 +define_bool CONFIG_GRKERNSEC_CHROOT_CHMOD n
3268 +define_bool CONFIG_GRKERNSEC_CHROOT_NICE n
3269 +define_bool CONFIG_GRKERNSEC_CHROOT_FINDTASK n
3270 +if [ "$CONFIG_X86" = "y" ]; then
3271 +define_bool CONFIG_GRKERNSEC_IO n
3272 +fi
3273 +define_bool CONFIG_GRKERNSEC_AUDIT_MOUNT n
3274 +define_bool CONFIG_GRKERNSEC_ACL_HIDEKERN n
3275 +define_bool CONFIG_GRKERNSEC_RESLOG n
3276 +define_int CONFIG_GRKERNSEC_ACL_MAXTRIES 3
3277 +define_int CONFIG_GRKERNSEC_ACL_TIMEOUT 30
3278 +
3279 +define_int  CONFIG_GRKERNSEC_FLOODTIME 10
3280 +define_int  CONFIG_GRKERNSEC_FLOODBURST 4
3281 +define_bool CONFIG_GRKERNSEC_LINK y
3282 +define_bool CONFIG_GRKERNSEC_FIFO y
3283 +define_bool CONFIG_GRKERNSEC_RANDPID y
3284 +define_bool CONFIG_GRKERNSEC_EXECVE y
3285 +define_bool CONFIG_GRKERNSEC_RANDNET y
3286 +define_bool CONFIG_GRKERNSEC_RANDISN n
3287 +define_bool CONFIG_GRKERNSEC_DMESG y
3288 +define_bool CONFIG_GRKERNSEC_RANDID y
3289 +define_bool CONFIG_GRKERNSEC_CHROOT_CHDIR y
3290 +fi
3291 +if [ "$CONFIG_GRKERNSEC_MID" = "y" ]; then
3292 +define_bool CONFIG_GRKERNSEC_KMEM n
3293 +define_bool CONFIG_GRKERNSEC_PROC_IPADDR n
3294 +define_bool CONFIG_GRKERNSEC_HIDESYM n
3295 +define_bool CONFIG_GRKERNSEC_PROC_ADD n
3296 +define_bool CONFIG_GRKERNSEC_CHROOT_CHMOD n
3297 +define_bool CONFIG_GRKERNSEC_CHROOT_NICE n
3298 +define_bool CONFIG_GRKERNSEC_CHROOT_FINDTASK n
3299 +if [ "$CONFIG_X86" = "y" ]; then
3300 +define_bool CONFIG_GRKERNSEC_IO n
3301 +fi
3302 +define_bool CONFIG_GRKERNSEC_AUDIT_MOUNT n
3303 +define_bool CONFIG_GRKERNSEC_CHROOT_CAPS n
3304 +define_bool CONFIG_GRKERNSEC_CHROOT_SYSCTL y
3305 +define_bool CONFIG_GRKERNSEC_AUDIT_MOUNT n
3306 +define_bool CONFIG_GRKERNSEC_CHROOT_FCHDIR n
3307 +define_bool CONFIG_GRKERNSEC_ACL_HIDEKERN n
3308 +define_bool CONFIG_GRKERNSEC_RESLOG n
3309 +define_int CONFIG_GRKERNSEC_ACL_MAXTRIES 3
3310 +define_int CONFIG_GRKERNSEC_ACL_TIMEOUT 30
3311 +
3312 +define_int  CONFIG_GRKERNSEC_FLOODTIME 10
3313 +define_int  CONFIG_GRKERNSEC_FLOODBURST 4
3314 +define_bool CONFIG_GRKERNSEC_LINK y
3315 +define_bool CONFIG_GRKERNSEC_FIFO y
3316 +define_bool CONFIG_GRKERNSEC_RANDPID y
3317 +define_bool CONFIG_GRKERNSEC_EXECVE y
3318 +define_bool CONFIG_GRKERNSEC_DMESG y
3319 +define_bool CONFIG_GRKERNSEC_RANDID y
3320 +define_bool CONFIG_GRKERNSEC_RANDNET y
3321 +define_bool CONFIG_GRKERNSEC_RANDISN y
3322 +define_bool CONFIG_GRKERNSEC_RANDSRC y
3323 +define_bool CONFIG_GRKERNSEC_RANDRPC y
3324 +define_bool CONFIG_GRKERNSEC_FORKFAIL y
3325 +define_bool CONFIG_GRKERNSEC_TIME y
3326 +define_bool CONFIG_GRKERNSEC_SIGNAL y
3327 +define_bool CONFIG_GRKERNSEC_CHROOT y
3328 +define_bool CONFIG_GRKERNSEC_CHROOT_SHMAT n
3329 +define_bool CONFIG_GRKERNSEC_CHROOT_UNIX y
3330 +define_bool CONFIG_GRKERNSEC_CHROOT_MOUNT y
3331 +define_bool CONFIG_GRKERNSEC_CHROOT_PIVOT y
3332 +define_bool CONFIG_GRKERNSEC_CHROOT_DOUBLE y
3333 +define_bool CONFIG_GRKERNSEC_CHROOT_CHDIR y
3334 +define_bool CONFIG_GRKERNSEC_CHROOT_MKNOD y
3335 +define_bool CONFIG_GRKERNSEC_PROC y
3336 +define_bool CONFIG_GRKERNSEC_PROC_USERGROUP y
3337 +define_int  CONFIG_GRKERNSEC_PROC_GID 10
3338 +fi
3339 +if [ "$CONFIG_GRKERNSEC_HI" = "y" ]; then
3340 +define_int CONFIG_GRKERNSEC_FLOODTIME 10
3341 +define_int  CONFIG_GRKERNSEC_FLOODBURST 4
3342 +define_bool CONFIG_GRKERNSEC_LINK y
3343 +define_bool CONFIG_GRKERNSEC_FIFO y
3344 +define_bool CONFIG_GRKERNSEC_RANDPID y
3345 +define_bool CONFIG_GRKERNSEC_EXECVE y
3346 +define_bool CONFIG_GRKERNSEC_DMESG y
3347 +define_bool CONFIG_GRKERNSEC_RANDID y
3348 +define_bool CONFIG_GRKERNSEC_RANDSRC y
3349 +define_bool CONFIG_GRKERNSEC_RANDRPC y
3350 +define_bool CONFIG_GRKERNSEC_FORKFAIL y
3351 +define_bool CONFIG_GRKERNSEC_TIME y
3352 +define_bool CONFIG_GRKERNSEC_SIGNAL y
3353 +define_bool CONFIG_GRKERNSEC_CHROOT_SHMAT y
3354 +define_bool CONFIG_GRKERNSEC_CHROOT_UNIX y
3355 +define_bool CONFIG_GRKERNSEC_CHROOT_MOUNT y
3356 +define_bool CONFIG_GRKERNSEC_CHROOT_FCHDIR y
3357 +define_bool CONFIG_GRKERNSEC_CHROOT_PIVOT y
3358 +define_bool CONFIG_GRKERNSEC_CHROOT_DOUBLE y
3359 +define_bool CONFIG_GRKERNSEC_CHROOT_CHDIR y
3360 +define_bool CONFIG_GRKERNSEC_CHROOT_MKNOD y
3361 +define_bool CONFIG_GRKERNSEC_CHROOT_CAPS y
3362 +define_bool CONFIG_GRKERNSEC_CHROOT_SYSCTL y
3363 +define_bool CONFIG_GRKERNSEC_CHROOT_FINDTASK y
3364 +define_bool CONFIG_GRKERNSEC_PROC y
3365 +define_bool CONFIG_GRKERNSEC_PROC_IPADDR n
3366 +define_bool CONFIG_GRKERNSEC_HIDESYM y
3367 +define_bool CONFIG_GRKERNSEC_PROC_USERGROUP y
3368 +define_int  CONFIG_GRKERNSEC_PROC_GID 10
3369 +define_bool CONFIG_GRKERNSEC_KMEM y
3370 +define_bool CONFIG_GRKERNSEC_RESLOG y
3371 +define_bool CONFIG_GRKERNSEC_RANDNET y
3372 +define_bool CONFIG_GRKERNSEC_RANDISN y
3373 +
3374 +define_bool CONFIG_GRKERNSEC_AUDIT_MOUNT n
3375 +define_bool CONFIG_GRKERNSEC_ACL_HIDEKERN n
3376 +define_int CONFIG_GRKERNSEC_ACL_MAXTRIES 3
3377 +define_int CONFIG_GRKERNSEC_ACL_TIMEOUT 30
3378 +
3379 +define_bool CONFIG_GRKERNSEC_PROC_ADD y
3380 +define_bool CONFIG_GRKERNSEC_CHROOT_CHMOD y
3381 +define_bool CONFIG_GRKERNSEC_CHROOT_NICE y
3382 +if [ "$CONFIG_X86" = "y" ]; then
3383 +define_bool CONFIG_GRKERNSEC_IO n
3384 +fi
3385 +define_bool CONFIG_GRKERNSEC_AUDIT_MOUNT y
3386 +fi
3387 +if [ "$CONFIG_GRKERNSEC_CUSTOM" = "y" ]; then
3388 +mainmenu_option next_comment
3389 +comment 'Address Space Protection'
3390 +bool 'Deny writing to /dev/kmem, /dev/mem, and /dev/port' CONFIG_GRKERNSEC_KMEM
3391 +if [ "$CONFIG_X86" = "y" ]; then
3392 +  bool 'Disable privileged I/O' CONFIG_GRKERNSEC_IO
3393 +  if [ "$CONFIG_GRKERNSEC_IO" = "y" ]; then
3394 +    define_bool CONFIG_RTC y
3395 +  fi
3396 +fi
3397 +bool 'Hide kernel symbols' CONFIG_GRKERNSEC_HIDESYM
3398 +endmenu
3399 +mainmenu_option next_comment
3400 +comment 'Role Based Access Control Options'
3401 +bool 'Hide kernel processes' CONFIG_GRKERNSEC_ACL_HIDEKERN
3402 +int 'Maximum tries before password lockout' CONFIG_GRKERNSEC_ACL_MAXTRIES 3
3403 +int 'Time to wait after max password tries, in seconds' CONFIG_GRKERNSEC_ACL_TIMEOUT 30
3404 +endmenu
3405 +mainmenu_option next_comment
3406 +comment 'Filesystem Protections'
3407 +bool 'Proc restrictions' CONFIG_GRKERNSEC_PROC
3408 +if [ "$CONFIG_GRKERNSEC_PROC" != "n" ]; then
3409 + bool '   Restrict to user only' CONFIG_GRKERNSEC_PROC_USER
3410 + if [ "$CONFIG_GRKERNSEC_PROC_USER" != "y" ]; then
3411 +  bool '   Allow special group' CONFIG_GRKERNSEC_PROC_USERGROUP
3412 +  if [ "$CONFIG_GRKERNSEC_PROC_USERGROUP" != "n" ]; then
3413 +   int  '   GID for special group' CONFIG_GRKERNSEC_PROC_GID 1001
3414 +  fi
3415 + fi
3416 + if [ "$CONFIG_GRKERNSEC_PROC_USER" != "n" -o "$CONFIG_GRKERNSEC_PROC_USERGROUP" != "n" ]; then
3417 +  bool '   Additional restrictions' CONFIG_GRKERNSEC_PROC_ADD
3418 + fi
3419 +fi
3420 +bool 'Linking restrictions' CONFIG_GRKERNSEC_LINK
3421 +bool 'FIFO restrictions' CONFIG_GRKERNSEC_FIFO
3422 +bool 'Chroot jail restrictions' CONFIG_GRKERNSEC_CHROOT
3423 +if [ "$CONFIG_GRKERNSEC_CHROOT" != "n" ]; then
3424 +bool '   Deny mounts' CONFIG_GRKERNSEC_CHROOT_MOUNT
3425 +bool '   Deny double-chroots' CONFIG_GRKERNSEC_CHROOT_DOUBLE
3426 +bool '   Deny pivot_root in chroot' CONFIG_GRKERNSEC_CHROOT_PIVOT
3427 +bool '   Enforce chdir("/") on all chroots' CONFIG_GRKERNSEC_CHROOT_CHDIR
3428 +bool '   Deny (f)chmod +s' CONFIG_GRKERNSEC_CHROOT_CHMOD
3429 +bool '   Deny fchdir out of chroot' CONFIG_GRKERNSEC_CHROOT_FCHDIR
3430 +bool '   Deny mknod' CONFIG_GRKERNSEC_CHROOT_MKNOD
3431 +bool '   Deny shmat() out of chroot' CONFIG_GRKERNSEC_CHROOT_SHMAT
3432 +bool '   Deny access to abstract AF_UNIX sockets out of chroot' CONFIG_GRKERNSEC_CHROOT_UNIX
3433 +bool '   Protect outside processes' CONFIG_GRKERNSEC_CHROOT_FINDTASK
3434 +bool '   Restrict priority changes' CONFIG_GRKERNSEC_CHROOT_NICE
3435 +bool '   Deny sysctl writes in chroot' CONFIG_GRKERNSEC_CHROOT_SYSCTL
3436 +bool '   Capability restrictions within chroot' CONFIG_GRKERNSEC_CHROOT_CAPS
3437 +fi
3438 +endmenu
3439 +mainmenu_option next_comment
3440 +comment 'Kernel Auditing'
3441 +bool 'Single group for auditing' CONFIG_GRKERNSEC_AUDIT_GROUP
3442 +if [ "$CONFIG_GRKERNSEC_AUDIT_GROUP" != "n" ]; then
3443 +int  '   GID for auditing' CONFIG_GRKERNSEC_AUDIT_GID 1007
3444 +fi
3445 +bool 'Exec logging' CONFIG_GRKERNSEC_EXECLOG
3446 +bool 'Resource logging' CONFIG_GRKERNSEC_RESLOG
3447 +bool 'Log execs within chroot' CONFIG_GRKERNSEC_CHROOT_EXECLOG
3448 +bool 'Chdir logging' CONFIG_GRKERNSEC_AUDIT_CHDIR
3449 +bool '(Un)Mount logging' CONFIG_GRKERNSEC_AUDIT_MOUNT
3450 +bool 'IPC logging' CONFIG_GRKERNSEC_AUDIT_IPC
3451 +bool 'Signal logging' CONFIG_GRKERNSEC_SIGNAL
3452 +bool 'Fork failure logging' CONFIG_GRKERNSEC_FORKFAIL
3453 +bool 'Time change logging' CONFIG_GRKERNSEC_TIME
3454 +bool '/proc/<pid>/ipaddr support' CONFIG_GRKERNSEC_PROC_IPADDR
3455 +endmenu
3456 +mainmenu_option next_comment
3457 +comment 'Executable Protections'
3458 +bool 'Enforce RLIMIT_NPROC on execs' CONFIG_GRKERNSEC_EXECVE
3459 +bool 'Dmesg(8) restriction' CONFIG_GRKERNSEC_DMESG
3460 +bool 'Randomized PIDs' CONFIG_GRKERNSEC_RANDPID
3461 +bool 'Trusted path execution' CONFIG_GRKERNSEC_TPE
3462 +if [ "$CONFIG_GRKERNSEC_TPE" != "n" ]; then
3463 +bool '   Partially restrict non-root users' CONFIG_GRKERNSEC_TPE_ALL
3464 +int  '   GID for untrusted users:' CONFIG_GRKERNSEC_TPE_GID 1005
3465 +fi
3466 +endmenu
3467 +mainmenu_option next_comment
3468 +comment 'Network Protections'
3469 +bool 'Larger entropy pools' CONFIG_GRKERNSEC_RANDNET
3470 +bool 'Truly random TCP ISN selection' CONFIG_GRKERNSEC_RANDISN
3471 +bool 'Randomized IP IDs' CONFIG_GRKERNSEC_RANDID
3472 +bool 'Randomized TCP source ports' CONFIG_GRKERNSEC_RANDSRC
3473 +bool 'Randomized RPC XIDs' CONFIG_GRKERNSEC_RANDRPC
3474 +bool 'Socket restrictions' CONFIG_GRKERNSEC_SOCKET
3475 +if [ "$CONFIG_GRKERNSEC_SOCKET" != "n" ]; then
3476 +bool '  Deny any sockets to group' CONFIG_GRKERNSEC_SOCKET_ALL
3477 +if [ "$CONFIG_GRKERNSEC_SOCKET_ALL" != "n" ]; then
3478 +int  '   GID to deny all sockets for:' CONFIG_GRKERNSEC_SOCKET_ALL_GID 1004
3479 +fi
3480 +bool '  Deny client sockets to group' CONFIG_GRKERNSEC_SOCKET_CLIENT
3481 +if [ "$CONFIG_GRKERNSEC_SOCKET_CLIENT" != "n" ]; then
3482 +int  '   GID to deny client sockets for:' CONFIG_GRKERNSEC_SOCKET_CLIENT_GID 1003
3483 +fi
3484 +bool '  Deny server sockets to group' CONFIG_GRKERNSEC_SOCKET_SERVER
3485 +if [ "$CONFIG_GRKERNSEC_SOCKET_SERVER" != "n" ]; then
3486 +int  '   GID to deny server sockets for:' CONFIG_GRKERNSEC_SOCKET_SERVER_GID 1002
3487 +fi
3488 +fi
3489 +endmenu
3490 +if [ "$CONFIG_SYSCTL" != "n" ]; then
3491 +mainmenu_option next_comment
3492 +comment 'Sysctl support'
3493 +bool 'Sysctl support' CONFIG_GRKERNSEC_SYSCTL
3494 +endmenu
3495 +fi
3496 +mainmenu_option next_comment
3497 +comment 'Logging options'
3498 +int 'Seconds in between log messages (minimum)' CONFIG_GRKERNSEC_FLOODTIME 10
3499 +int 'Number of messages in a burst (maximum)' CONFIG_GRKERNSEC_FLOODBURST 4
3500 +endmenu
3501 +fi
3502 diff -urN linux-2.4.21/grsecurity/Makefile linux-2.4.21/grsecurity/Makefile
3503 --- linux-2.4.21/grsecurity/Makefile    1969-12-31 19:00:00.000000000 -0500
3504 +++ linux-2.4.21/grsecurity/Makefile    2003-06-23 11:49:16.000000000 -0400
3505 @@ -0,0 +1,24 @@
3506 +# grsecurity's ACL system was originally written in 2001 by Michael Dalton
3507 +# during 2001, 2002, and 2003 it has been completely redesigned by
3508 +# Brad Spengler
3509 +#
3510 +# All code in this directory and various hooks inserted throughout the kernel
3511 +# are copyright Brad Spengler, and released under the GPL, unless otherwise
3512 +# noted (as in obsd_rand.c)
3513 +
3514 +O_TARGET := grsec.o
3515 +
3516 +obj-y = grsec_chdir.o grsec_chroot.o grsec_exec.o grsec_fifo.o grsec_fork.o \
3517 +       grsec_mount.o grsec_rand.o grsec_sig.o grsec_sock.o grsec_sysctl.o \
3518 +       grsec_time.o grsec_tpe.o grsec_ipc.o grsec_link.o
3519 +
3520 +ifeq ($(CONFIG_GRKERNSEC),y)
3521 +obj-y += grsec_init.o grsum.o gracl.o gracl_ip.o gracl_segv.o obsd_rand.o \
3522 +       gracl_cap.o gracl_alloc.o gracl_shm.o grsec_mem.o gracl_fs.o \
3523 +       gracl_learn.o
3524 +obj-$(CONFIG_GRKERNSEC_RESLOG) += gracl_res.o
3525 +else
3526 +obj-y += grsec_disabled.o
3527 +endif
3528 +
3529 +include $(TOPDIR)/Rules.make
3530 diff -urN linux-2.4.21/grsecurity/gracl.c linux-2.4.21/grsecurity/gracl.c
3531 --- linux-2.4.21/grsecurity/gracl.c     1969-12-31 19:00:00.000000000 -0500
3532 +++ linux-2.4.21/grsecurity/gracl.c     2003-07-29 17:58:19.000000000 -0400
3533 @@ -0,0 +1,2653 @@
3534 +/* 
3535 + * grsecurity/gracl.c
3536 + * Copyright Brad Spengler 2001, 2002, 2003
3537 + *
3538 + */
3539 +
3540 +#include <linux/kernel.h>
3541 +#include <linux/sched.h>
3542 +#include <linux/mm.h>
3543 +#include <linux/file.h>
3544 +#include <linux/fs.h>
3545 +#include <linux/proc_fs.h>
3546 +#include <linux/smp_lock.h>
3547 +#include <linux/slab.h>
3548 +#include <linux/vmalloc.h>
3549 +#include <linux/types.h>
3550 +#include <linux/capability.h>
3551 +#include <linux/sysctl.h>
3552 +#include <linux/gracl.h>
3553 +#include <linux/gralloc.h>
3554 +#include <linux/grsecurity.h>
3555 +#include <linux/grinternal.h>
3556 +
3557 +#include <asm/uaccess.h>
3558 +#include <asm/errno.h>
3559 +#include <asm/mman.h>
3560 +
3561 +static struct acl_role_db acl_role_set;
3562 +static struct acl_role_label *role_list_head;
3563 +static struct name_db name_set;
3564 +static struct name_db inodev_set;
3565 +
3566 +static struct acl_role_label *default_role;
3567 +
3568 +static u16 acl_sp_role_value;
3569 +
3570 +static DECLARE_MUTEX(gr_dev_sem);
3571 +rwlock_t gr_inode_lock = RW_LOCK_UNLOCKED;
3572 +
3573 +extern char *gr_shared_page[2][NR_CPUS];
3574 +
3575 +static unsigned long gr_status = GR_STATUS_INIT;
3576 +
3577 +extern int chkpw(struct gr_arg *entry, unsigned char *salt, unsigned char *sum);
3578 +extern void gr_clear_learn_entries(void);
3579 +
3580 +#ifdef CONFIG_GRKERNSEC_RESLOG
3581 +extern __inline__ void gr_log_resource(const struct task_struct *task,
3582 +                                      const int res,
3583 +                                      const unsigned long wanted);
3584 +#endif
3585 +
3586 +static unsigned char system_salt[GR_SALT_LEN];
3587 +static unsigned char system_sum[GR_SHA_LEN];
3588 +
3589 +static struct sprole_pw **acl_special_roles = NULL;
3590 +static __u16 num_sprole_pws = 0;
3591 +
3592 +static struct acl_role_label *kernel_role = NULL;
3593 +
3594 +/* The following are used to keep a place held in the hash table when we move
3595 +   entries around.  They can be replaced during insert. */
3596 +
3597 +static struct acl_subject_label *deleted_subject;
3598 +static struct acl_object_label *deleted_object;
3599 +static struct name_entry *deleted_inodev;
3600 +
3601 +/* for keeping track of the last and final allocated subjects, since
3602 +   nested subject parsing is tricky
3603 +*/
3604 +static struct acl_subject_label *s_last = NULL;
3605 +static struct acl_subject_label *s_final = NULL;
3606 +
3607 +static unsigned int gr_auth_attempts = 0;
3608 +static unsigned long gr_auth_expires = 0UL;
3609 +
3610 +extern int gr_init_uidset(void);
3611 +extern void gr_free_uidset(void);
3612 +extern void gr_remove_uid(uid_t uid);
3613 +extern int gr_find_uid(uid_t uid);
3614 +
3615 +__inline__ int
3616 +gr_acl_is_enabled(void)
3617 +{
3618 +       return (gr_status & GR_READY);
3619 +}
3620 +
3621 +static __inline__ int
3622 +gr_streq(const char *a, const char *b, const __u16 lena, const __u16 lenb)
3623 +{
3624 +       int i;
3625 +       unsigned long *l1;
3626 +       unsigned long *l2;
3627 +       unsigned char *c1;
3628 +       unsigned char *c2;
3629 +       int num_longs;
3630 +
3631 +       if (likely(lena != lenb))
3632 +               return 0;
3633 +
3634 +       l1 = (unsigned long *)a;
3635 +       l2 = (unsigned long *)b;
3636 +
3637 +       num_longs = lena / sizeof(unsigned long);
3638 +
3639 +       for (i = num_longs; i--; l1++, l2++) {
3640 +               if (unlikely(*l1 != *l2))
3641 +                       return 0;
3642 +       }
3643 +
3644 +       c1 = (unsigned char *) l1;
3645 +       c2 = (unsigned char *) l2;
3646 +
3647 +       i = lena - (num_longs * sizeof(unsigned long)); 
3648 +
3649 +       for (; i--; c1++, c2++) {
3650 +               if (unlikely(*c1 != *c2))
3651 +                       return 0;
3652 +       }
3653 +
3654 +       return 1;
3655 +}
3656 +               
3657 +static __inline__ char *
3658 +d_real_path(const struct dentry *dentry, const struct vfsmount *vfsmnt,
3659 +           char *buf, int buflen)
3660 +{
3661 +       char *res;
3662 +       struct dentry *our_dentry;
3663 +       struct vfsmount *our_mount;
3664 +       struct vfsmount *rootmnt;
3665 +       struct dentry *root;
3666 +
3667 +       our_dentry = (struct dentry *) dentry;
3668 +       our_mount = (struct vfsmount *) vfsmnt;
3669 +
3670 +       read_lock(&child_reaper->fs->lock);
3671 +       rootmnt = mntget(child_reaper->fs->rootmnt);
3672 +       root = dget(child_reaper->fs->root);
3673 +       read_unlock(&child_reaper->fs->lock);
3674 +
3675 +       spin_lock(&dcache_lock);
3676 +       res = __d_path(our_dentry, our_mount, root, rootmnt, buf, buflen);
3677 +       spin_unlock(&dcache_lock);
3678 +       dput(root);
3679 +       mntput(rootmnt);
3680 +       return res;
3681 +}
3682 +
3683 +char *
3684 +gr_to_filename(const struct dentry *dentry, const struct vfsmount *mnt)
3685 +{
3686 +       return d_real_path(dentry, mnt, gr_shared_page[0][smp_processor_id()],
3687 +                          PAGE_SIZE);
3688 +}
3689 +
3690 +char *
3691 +gr_to_filename1(const struct dentry *dentry, const struct vfsmount *mnt)
3692 +{
3693 +       return d_real_path(dentry, mnt, gr_shared_page[1][smp_processor_id()],
3694 +                          PAGE_SIZE);
3695 +}
3696 +
3697 +__inline__ __u32
3698 +to_gr_audit(const __u32 reqmode)
3699 +{
3700 +       __u32 retmode = 0;
3701 +
3702 +       retmode |= (reqmode & GR_READ) ? GR_AUDIT_READ : 0;
3703 +       retmode |= (reqmode & GR_WRITE) ? GR_AUDIT_WRITE | GR_AUDIT_APPEND : 0;
3704 +       retmode |= (reqmode & GR_APPEND) ? GR_AUDIT_APPEND : 0;
3705 +       retmode |= (reqmode & GR_EXEC) ? GR_AUDIT_EXEC : 0;
3706 +       retmode |= (reqmode & GR_INHERIT) ? GR_AUDIT_INHERIT : 0;
3707 +       retmode |= (reqmode & GR_FIND) ? GR_AUDIT_FIND : 0;
3708 +       retmode |= (reqmode & GR_SETID) ? GR_AUDIT_SETID : 0;
3709 +       retmode |= (reqmode & GR_CREATE) ? GR_AUDIT_CREATE : 0;
3710 +       retmode |= (reqmode & GR_DELETE) ? GR_AUDIT_DELETE : 0;
3711 +
3712 +       return retmode;
3713 +}
3714 +
3715 +__inline__ struct acl_role_label *
3716 +lookup_acl_role_label(const struct task_struct *task, const uid_t uid,
3717 +                     const gid_t gid)
3718 +{
3719 +       unsigned long index = rhash(uid, GR_ROLE_USER, acl_role_set.r_size);
3720 +       struct acl_role_label *match;
3721 +       struct role_allowed_ip *ipp;
3722 +       __u8 i = 0;
3723 +
3724 +       match = acl_role_set.r_hash[index];
3725 +
3726 +       while (match
3727 +              && (match->uidgid != uid || !(match->roletype & GR_ROLE_USER))) {
3728 +               index = (index + (1 << i)) % acl_role_set.r_size;
3729 +               match = acl_role_set.r_hash[index];
3730 +               i = (i + 1) % 32;
3731 +       }
3732 +
3733 +       if (!match || match->uidgid != uid || !(match->roletype & GR_ROLE_USER)) {
3734 +             try_group:
3735 +               index = rhash(gid, GR_ROLE_GROUP, acl_role_set.r_size);
3736 +               match = acl_role_set.r_hash[index];
3737 +               i = 0;
3738 +
3739 +               while (match
3740 +                      && (match->uidgid != gid
3741 +                          || !(match->roletype & GR_ROLE_GROUP))) {
3742 +                       index = (index + (1 << i)) % acl_role_set.r_size;
3743 +                       match = acl_role_set.r_hash[index];
3744 +                       i = (i + 1) % 32;
3745 +               }
3746 +
3747 +               if (!match || match->uidgid != gid
3748 +                   || !(match->roletype & GR_ROLE_GROUP))
3749 +                       match = default_role;
3750 +               else if (likely(!match->allowed_ips)) {
3751 +                       return match;
3752 +               } else {
3753 +                       for (ipp = match->allowed_ips; ipp; ipp = ipp->next) {
3754 +                               if (likely
3755 +                                   ((task->curr_ip & ipp->netmask) ==
3756 +                                    (ipp->addr & ipp->netmask)))
3757 +                                       return match;
3758 +                       }
3759 +                       match = default_role;
3760 +               }
3761 +       } else if (likely(!match->allowed_ips)) {
3762 +               return match;
3763 +       } else {
3764 +               for (ipp = match->allowed_ips; ipp; ipp = ipp->next) {
3765 +                       if (likely
3766 +                           ((task->curr_ip & ipp->netmask) ==
3767 +                            (ipp->addr & ipp->netmask)))
3768 +                               return match;
3769 +               }
3770 +               goto try_group;
3771 +       }
3772 +
3773 +       return match;
3774 +}
3775 +
3776 +__inline__ struct acl_subject_label *
3777 +lookup_acl_subj_label(const ino_t ino, const kdev_t dev,
3778 +                     const struct acl_role_label *role)
3779 +{
3780 +       unsigned long subj_size = role->subj_hash_size;
3781 +       struct acl_subject_label **s_hash = role->subj_hash;
3782 +       unsigned long index = fhash(ino, dev, subj_size);
3783 +       struct acl_subject_label *match;
3784 +       __u8 i = 0;
3785 +
3786 +       match = s_hash[index];
3787 +
3788 +       while (match && (match->inode != ino || match->device != dev ||
3789 +              (match->mode & GR_DELETED))) {
3790 +               index = (index + (1 << i)) % subj_size;
3791 +               match = s_hash[index];
3792 +               i = (i + 1) % 32;
3793 +       }
3794 +
3795 +       if (unlikely(match && (match != deleted_subject) &&
3796 +                    (match->inode == ino) && (match->device == dev) &&
3797 +                    !(match->mode & GR_DELETED)))
3798 +               return match;
3799 +       else
3800 +               return NULL;
3801 +}
3802 +
3803 +static __inline__ struct acl_object_label *
3804 +lookup_acl_obj_label(const ino_t ino, const kdev_t dev,
3805 +                    const struct acl_subject_label *subj)
3806 +{
3807 +       unsigned long obj_size = subj->obj_hash_size;
3808 +       struct acl_object_label **o_hash = subj->obj_hash;
3809 +       unsigned long index = fhash(ino, dev, obj_size);
3810 +       struct acl_object_label *match;
3811 +       __u8 i = 0;
3812 +
3813 +       match = o_hash[index];
3814 +
3815 +       while (match && (match->inode != ino || match->device != dev ||
3816 +              (match->mode & GR_DELETED))) {
3817 +               index = (index + (1 << i)) % obj_size;
3818 +               match = o_hash[index];
3819 +               i = (i + 1) % 32;
3820 +       }
3821 +
3822 +       if (unlikely(match && (match != deleted_object) &&
3823 +                    (match->inode == ino) && (match->device == dev) &&
3824 +                    !(match->mode & GR_DELETED)))
3825 +               return match;
3826 +       else
3827 +               return NULL;
3828 +}
3829 +
3830 +static __inline__ struct acl_object_label *
3831 +lookup_acl_obj_label_create(const ino_t ino, const kdev_t dev,
3832 +                    const struct acl_subject_label *subj)
3833 +{
3834 +       unsigned long obj_size = subj->obj_hash_size;
3835 +       struct acl_object_label **o_hash = subj->obj_hash;
3836 +       unsigned long index = fhash(ino, dev, obj_size);
3837 +       struct acl_object_label *match;
3838 +       __u8 i = 0;
3839 +
3840 +       match = o_hash[index];
3841 +
3842 +       while (match && (match->inode != ino || match->device != dev ||
3843 +              !(match->mode & GR_DELETED))) {
3844 +               index = (index + (1 << i)) % obj_size;
3845 +               match = o_hash[index];
3846 +               i = (i + 1) % 32;
3847 +       }
3848 +
3849 +       if (unlikely(match && (match != deleted_object) &&
3850 +                    (match->inode == ino) && (match->device == dev) &&
3851 +                    (match->mode & GR_DELETED)))
3852 +               return match;
3853 +
3854 +       i = 0;
3855 +       index = fhash(ino, dev, obj_size);
3856 +       match = o_hash[index];
3857 +
3858 +       while (match && (match->inode != ino || match->device != dev ||
3859 +              (match->mode & GR_DELETED))) {
3860 +               index = (index + (1 << i)) % obj_size;
3861 +               match = o_hash[index];
3862 +               i = (i + 1) % 32;
3863 +       }
3864 +
3865 +       if (unlikely(match && (match != deleted_object) &&
3866 +                    (match->inode == ino) && (match->device == dev) &&
3867 +                    !(match->mode & GR_DELETED)))
3868 +               return match;
3869 +       else
3870 +               return NULL;
3871 +}
3872 +
3873 +static __inline__ struct name_entry *
3874 +lookup_name_entry(const char *name)
3875 +{
3876 +       __u16 len = strlen(name);
3877 +       unsigned long index = nhash(name, len, name_set.n_size);
3878 +       struct name_entry *match;
3879 +       __u8 i = 0;
3880 +
3881 +       match = name_set.n_hash[index];
3882 +
3883 +       while (match && !gr_streq(match->name, name, match->len, len)) {
3884 +               index = (index + (1 << i)) % name_set.n_size;
3885 +               match = name_set.n_hash[index];
3886 +               i = (i + 1) % 32;
3887 +       }
3888 +
3889 +       if (unlikely(!match || !gr_streq(match->name, name, match->len, len)))
3890 +               return NULL;
3891 +       else
3892 +               return match;
3893 +}
3894 +
3895 +static __inline__ struct name_entry *
3896 +lookup_inodev_entry(const ino_t ino, const kdev_t dev)
3897 +{
3898 +       unsigned long index = fhash(ino, dev, inodev_set.n_size);
3899 +       struct name_entry *match;
3900 +       __u8 i = 0;
3901 +
3902 +       match = inodev_set.n_hash[index];
3903 +
3904 +       while (match && (match->inode != ino || match->device != dev)) {
3905 +               index = (index + (1 << i)) % inodev_set.n_size;
3906 +               match = inodev_set.n_hash[index];
3907 +               i = (i + 1) % 32;
3908 +       }
3909 +
3910 +       if (unlikely(match && (match != deleted_inodev) &&
3911 +                    (match->inode == ino) && (match->device == dev)))
3912 +               return match;
3913 +       else
3914 +               return NULL;
3915 +}
3916 +
3917 +static void
3918 +insert_inodev_entry(struct name_entry *nentry)
3919 +{
3920 +       unsigned long index = fhash(nentry->inode, nentry->device,
3921 +                                   inodev_set.n_size);
3922 +       struct name_entry **curr;
3923 +       __u8 i = 0;
3924 +
3925 +       curr = &inodev_set.n_hash[index];
3926 +
3927 +       while (*curr && *curr != deleted_inodev) {
3928 +               index = (index + (1 << i)) % inodev_set.n_size;
3929 +               curr = &inodev_set.n_hash[index];
3930 +               i = (i + 1) % 32;
3931 +       }
3932 +
3933 +       *curr = nentry;
3934 +
3935 +       return;
3936 +}
3937 +
3938 +static void
3939 +insert_acl_role_label(struct acl_role_label *role)
3940 +{
3941 +       unsigned long index =
3942 +           rhash(role->uidgid, role->roletype & (GR_ROLE_USER | GR_ROLE_GROUP), acl_role_set.r_size);
3943 +       struct acl_role_label **curr;
3944 +       __u8 i = 0;
3945 +
3946 +       curr = &acl_role_set.r_hash[index];
3947 +
3948 +       while (*curr) {
3949 +               index = (index + (1 << i)) % acl_role_set.r_size;
3950 +               curr = &acl_role_set.r_hash[index];
3951 +               i = (i + 1) % 32;
3952 +       }
3953 +
3954 +       *curr = role;
3955 +
3956 +       return;
3957 +}
3958 +
3959 +static int
3960 +insert_name_entry(char *name, const ino_t inode, const kdev_t device)
3961 +{
3962 +       struct name_entry **curr;
3963 +       __u8 i = 0;
3964 +       __u16 len = strlen(name);
3965 +       unsigned long index = nhash(name, len, name_set.n_size);
3966 +
3967 +       curr = &name_set.n_hash[index];
3968 +
3969 +       while (*curr && !gr_streq((*curr)->name, name, (*curr)->len, len)) {
3970 +               index = (index + (1 << i)) % name_set.n_size;
3971 +               curr = &name_set.n_hash[index];
3972 +               i = (i + 1) % 32;
3973 +       }
3974 +
3975 +       if (!(*curr)) {
3976 +               struct name_entry *nentry =
3977 +                   acl_alloc(sizeof (struct name_entry));
3978 +               if (!nentry)
3979 +                       return 0;
3980 +               nentry->name = name;
3981 +               nentry->inode = inode;
3982 +               nentry->device = device;
3983 +               nentry->len = len;
3984 +               *curr = nentry;
3985 +               /* insert us into the table searchable by inode/dev */
3986 +               insert_inodev_entry(nentry);
3987 +       }
3988 +
3989 +       return 1;
3990 +}
3991 +
3992 +static void
3993 +insert_acl_obj_label(struct acl_object_label *obj,
3994 +                    struct acl_subject_label *subj)
3995 +{
3996 +       unsigned long index =
3997 +           fhash(obj->inode, obj->device, subj->obj_hash_size);
3998 +       struct acl_object_label **curr;
3999 +       __u8 i = 0;
4000 +
4001 +       curr = &subj->obj_hash[index];
4002 +
4003 +       while (*curr && *curr != deleted_object) {
4004 +               index = (index + (1 << i)) % subj->obj_hash_size;
4005 +               curr = &subj->obj_hash[index];
4006 +               i = (i + 1) % 32;
4007 +       }
4008 +
4009 +       *curr = obj;
4010 +
4011 +       return;
4012 +}
4013 +
4014 +static void
4015 +insert_acl_subj_label(struct acl_subject_label *obj,
4016 +                     struct acl_role_label *role)
4017 +{
4018 +       unsigned long subj_size = role->subj_hash_size;
4019 +       struct acl_subject_label **s_hash = role->subj_hash;
4020 +       unsigned long index = fhash(obj->inode, obj->device, subj_size);
4021 +       struct acl_subject_label **curr;
4022 +       __u8 i = 0;
4023 +
4024 +       curr = &s_hash[index];
4025 +
4026 +       while (*curr && *curr != deleted_subject) {
4027 +               index = (index + (1 << i)) % subj_size;
4028 +               curr = &s_hash[index];
4029 +               i = (i + 1) % 32;
4030 +       }
4031 +
4032 +       *curr = obj;
4033 +
4034 +       return;
4035 +}
4036 +
4037 +static void **
4038 +create_table(__u32 * len)
4039 +{
4040 +       unsigned long table_sizes[] = {
4041 +               7, 13, 31, 61, 127, 251, 509, 1021, 2039, 4093, 8191, 16381,
4042 +               32749, 65521, 131071, 262139, 524287, 1048573, 2097143,
4043 +               4194301, 8388593, 16777213, 33554393, 67108859, 134217689,
4044 +               268435399, 536870909, 1073741789, 2147483647
4045 +       };
4046 +       void *newtable = NULL;
4047 +       unsigned int pwr = 0;
4048 +
4049 +       while ((pwr < ((sizeof (table_sizes) / sizeof (table_sizes[0])) - 1)) &&
4050 +              table_sizes[pwr] <= (2 * (*len)))
4051 +               pwr++;
4052 +
4053 +       if (table_sizes[pwr] <= (2 * (*len)))
4054 +               return newtable;
4055 +
4056 +       if ((table_sizes[pwr] * sizeof (void *)) <= PAGE_SIZE)
4057 +               newtable =
4058 +                   kmalloc(table_sizes[pwr] * sizeof (void *), GFP_KERNEL);
4059 +       else
4060 +               newtable = vmalloc(table_sizes[pwr] * sizeof (void *));
4061 +
4062 +       *len = table_sizes[pwr];
4063 +
4064 +       return newtable;
4065 +}
4066 +
4067 +static int
4068 +init_variables(const unsigned long acl_obj_size,
4069 +              const unsigned long acl_subj_size,
4070 +              const unsigned long acl_ip_size,
4071 +              const unsigned long acl_role_size,
4072 +              const unsigned long allowed_ip_size,
4073 +              const unsigned long acl_trans_size,
4074 +              const __u16 num_sprole_pws)
4075 +{
4076 +       unsigned long stacksize;
4077 +
4078 +       acl_role_set.r_size = acl_role_size;
4079 +       name_set.n_size = (acl_obj_size + acl_subj_size);
4080 +       inodev_set.n_size = (acl_obj_size + acl_subj_size);
4081 +
4082 +       if (!gr_init_uidset())
4083 +               return 1;
4084 +
4085 +       /* set up the stack that holds allocation info */
4086 +
4087 +       stacksize = (3 * acl_obj_size) + (2 * acl_role_size) +
4088 +           (4 * acl_subj_size) + acl_ip_size + (2 * acl_trans_size) +
4089 +           allowed_ip_size + (2 * num_sprole_pws) + 5;
4090 +
4091 +       if (!acl_alloc_stack_init(stacksize))
4092 +               return 1;
4093 +
4094 +       /* create our empty, fake deleted acls */
4095 +       deleted_subject =
4096 +           (struct acl_subject_label *)
4097 +           acl_alloc(sizeof (struct acl_subject_label));
4098 +       deleted_object =
4099 +           (struct acl_object_label *)
4100 +           acl_alloc(sizeof (struct acl_object_label));
4101 +       deleted_inodev =
4102 +           (struct name_entry *) acl_alloc(sizeof (struct name_entry));
4103 +
4104 +       if (!deleted_subject || !deleted_object || !deleted_inodev)
4105 +               return 1;
4106 +
4107 +       memset(deleted_subject, 0, sizeof (struct acl_subject_label));
4108 +       memset(deleted_object, 0, sizeof (struct acl_object_label));
4109 +       memset(deleted_inodev, 0, sizeof (struct name_entry));
4110 +
4111 +       /* We only want 50% full tables for now */
4112 +
4113 +       acl_role_set.r_hash =
4114 +           (struct acl_role_label **) create_table(&acl_role_set.r_size);
4115 +       name_set.n_hash = (struct name_entry **) create_table(&name_set.n_size);
4116 +       inodev_set.n_hash =
4117 +           (struct name_entry **) create_table(&inodev_set.n_size);
4118 +
4119 +       if (!acl_role_set.r_hash || !name_set.n_hash || !inodev_set.n_hash)
4120 +               return 1;
4121 +       memset(acl_role_set.r_hash, 0,
4122 +              sizeof (struct acl_role_label *) * acl_role_set.r_size);
4123 +       memset(name_set.n_hash, 0,
4124 +              sizeof (struct name_entry *) * name_set.n_size);
4125 +       memset(inodev_set.n_hash, 0,
4126 +              sizeof (struct name_entry *) * inodev_set.n_size);
4127 +
4128 +       return 0;
4129 +}
4130 +
4131 +static void
4132 +free_variables(void)
4133 +{
4134 +       struct acl_subject_label *s;
4135 +       struct acl_role_label *r;
4136 +       struct task_struct *task;
4137 +
4138 +       gr_clear_learn_entries();
4139 +
4140 +       read_lock(&tasklist_lock);
4141 +       for_each_task(task) {
4142 +               task->acl_sp_role = 0;
4143 +               task->acl_role_id = 0;
4144 +               task->acl = NULL;
4145 +               task->role = NULL;
4146 +       }
4147 +       read_unlock(&tasklist_lock);
4148 +
4149 +       /* free all object hash tables */
4150 +
4151 +       if (role_list_head) {
4152 +               for (r = role_list_head; r; r = r->next) {
4153 +                       if (!r->subj_hash)
4154 +                               break;
4155 +                       for (s = r->proc_subject; s; s = s->next) {
4156 +                               if (!s->obj_hash)
4157 +                                       break;
4158 +                               if ((s->obj_hash_size *
4159 +                                    sizeof (struct acl_object_label *)) <=
4160 +                                   PAGE_SIZE)
4161 +                                       kfree(s->obj_hash);
4162 +                               else
4163 +                                       vfree(s->obj_hash);
4164 +                       }
4165 +                       if ((r->subj_hash_size *
4166 +                            sizeof (struct acl_subject_label *)) <= PAGE_SIZE)
4167 +                               kfree(r->subj_hash);
4168 +                       else
4169 +                               vfree(r->subj_hash);
4170 +               }
4171 +       }
4172 +
4173 +       acl_free_all();
4174 +
4175 +       if (acl_role_set.r_hash) {
4176 +               if ((acl_role_set.r_size * sizeof (struct acl_role_label *)) <=
4177 +                   PAGE_SIZE)
4178 +                       kfree(acl_role_set.r_hash);
4179 +               else
4180 +                       vfree(acl_role_set.r_hash);
4181 +       }
4182 +       if (name_set.n_hash) {
4183 +               if ((name_set.n_size * sizeof (struct name_entry *)) <=
4184 +                   PAGE_SIZE)
4185 +                       kfree(name_set.n_hash);
4186 +               else
4187 +                       vfree(name_set.n_hash);
4188 +       }
4189 +
4190 +       if (inodev_set.n_hash) {
4191 +               if ((inodev_set.n_size * sizeof (struct name_entry *)) <=
4192 +                   PAGE_SIZE)
4193 +                       kfree(inodev_set.n_hash);
4194 +               else
4195 +                       vfree(inodev_set.n_hash);
4196 +       }
4197 +
4198 +       gr_free_uidset();
4199 +
4200 +       memset(&name_set, 0, sizeof (struct name_db));
4201 +       memset(&inodev_set, 0, sizeof (struct name_db));
4202 +       memset(&acl_role_set, 0, sizeof (struct acl_role_db));
4203 +
4204 +       role_list_head = NULL;
4205 +       default_role = NULL;
4206 +
4207 +       return;
4208 +}
4209 +
4210 +static __u32
4211 +count_user_objs(struct acl_object_label *userp)
4212 +{
4213 +       struct acl_object_label o_tmp;
4214 +       __u32 num = 0;
4215 +
4216 +       while (userp) {
4217 +               if (copy_from_user(&o_tmp, userp,
4218 +                                  sizeof (struct acl_object_label)))
4219 +                       break;
4220 +
4221 +               userp = o_tmp.prev;
4222 +               num++;
4223 +       }
4224 +
4225 +       return num;
4226 +}
4227 +
4228 +static struct acl_subject_label *
4229 +do_copy_user_subj(struct acl_subject_label *userp, struct acl_role_label *role);
4230 +
4231 +static int
4232 +copy_user_objs(struct acl_object_label *userp, struct acl_subject_label *subj,
4233 +              struct acl_role_label *role)
4234 +{
4235 +       struct acl_object_label *o_tmp;
4236 +       unsigned int len;
4237 +       char *tmp;
4238 +
4239 +       while (userp) {
4240 +               if ((o_tmp = (struct acl_object_label *)
4241 +                    acl_alloc(sizeof (struct acl_object_label))) == NULL)
4242 +                       return -ENOMEM;
4243 +
4244 +               if (copy_from_user(o_tmp, userp,
4245 +                                  sizeof (struct acl_object_label)))
4246 +                       return -EFAULT;
4247 +
4248 +               userp = o_tmp->prev;
4249 +
4250 +               len = strnlen_user(o_tmp->filename, PATH_MAX);
4251 +
4252 +               if (!len || len >= PATH_MAX)
4253 +                       return -EINVAL;
4254 +
4255 +               if ((tmp = (char *) acl_alloc(len)) == NULL)
4256 +                       return -ENOMEM;
4257 +
4258 +               if (copy_from_user(tmp, o_tmp->filename, len))
4259 +                       return -EFAULT;
4260 +
4261 +               o_tmp->filename = tmp;
4262 +
4263 +               insert_acl_obj_label(o_tmp, subj);
4264 +               if (!insert_name_entry(o_tmp->filename, o_tmp->inode,
4265 +                                      o_tmp->device))
4266 +                       return -ENOMEM;
4267 +
4268 +               if (o_tmp->nested) {
4269 +                       o_tmp->nested = do_copy_user_subj(o_tmp->nested, role);
4270 +                       if (IS_ERR(o_tmp->nested))
4271 +                               return PTR_ERR(o_tmp->nested);
4272 +
4273 +                       s_final = o_tmp->nested;
4274 +               }
4275 +       }
4276 +
4277 +       return 0;
4278 +}
4279 +
4280 +static __u32
4281 +count_user_subjs(struct acl_subject_label *userp)
4282 +{
4283 +       struct acl_subject_label s_tmp;
4284 +       __u32 num = 0;
4285 +
4286 +       while (userp) {
4287 +               if (copy_from_user(&s_tmp, userp,
4288 +                                  sizeof (struct acl_subject_label)))
4289 +                       break;
4290 +
4291 +               userp = s_tmp.prev;
4292 +               /* do not count nested subjects against this count, since
4293 +                  they are not included in the hash table, but are
4294 +                  attached to objects.  We have already counted
4295 +                  the subjects in userspace for the allocation 
4296 +                  stack
4297 +               */
4298 +               if (!s_tmp.parent_subject)
4299 +                       num++;
4300 +       }
4301 +
4302 +       return num;
4303 +}
4304 +
4305 +static int
4306 +copy_user_allowedips(struct acl_role_label *rolep)
4307 +{
4308 +       struct role_allowed_ip *ruserip, *rtmp = NULL, *rlast;
4309 +
4310 +       ruserip = rolep->allowed_ips;
4311 +
4312 +       while (ruserip) {
4313 +               rlast = rtmp;
4314 +
4315 +               if ((rtmp = (struct role_allowed_ip *)
4316 +                    acl_alloc(sizeof (struct role_allowed_ip))) == NULL)
4317 +                       return -ENOMEM;
4318 +
4319 +               if (copy_from_user(rtmp, ruserip,
4320 +                                  sizeof (struct role_allowed_ip)))
4321 +                       return -EFAULT;
4322 +
4323 +               ruserip = rtmp->prev;
4324 +
4325 +               if (!rlast) {
4326 +                       rtmp->prev = NULL;
4327 +                       rolep->allowed_ips = rtmp;
4328 +               } else {
4329 +                       rlast->next = rtmp;
4330 +                       rtmp->prev = rlast;
4331 +               }
4332 +
4333 +               if (!ruserip)
4334 +                       rtmp->next = NULL;
4335 +       }
4336 +
4337 +       return 0;
4338 +}
4339 +
4340 +static int
4341 +copy_user_transitions(struct acl_role_label *rolep)
4342 +{
4343 +       struct role_transition *rusertp, *rtmp = NULL, *rlast;
4344 +       unsigned int len;
4345 +       char *tmp;
4346 +
4347 +       rusertp = rolep->transitions;
4348 +
4349 +       while (rusertp) {
4350 +               rlast = rtmp;
4351 +
4352 +               if ((rtmp = (struct role_transition *)
4353 +                    acl_alloc(sizeof (struct role_transition))) == NULL)
4354 +                       return -ENOMEM;
4355 +
4356 +               if (copy_from_user(rtmp, rusertp,
4357 +                                  sizeof (struct role_transition)))
4358 +                       return -EFAULT;
4359 +
4360 +               rusertp = rtmp->prev;
4361 +
4362 +               len = strnlen_user(rtmp->rolename, GR_SPROLE_LEN);
4363 +
4364 +               if (!len || len >= GR_SPROLE_LEN)
4365 +                       return -EINVAL;
4366 +
4367 +               if ((tmp = (char *) acl_alloc(len)) == NULL)
4368 +                       return -ENOMEM;
4369 +
4370 +               if (copy_from_user(tmp, rtmp->rolename, len))
4371 +                       return -EFAULT;
4372 +
4373 +               rtmp->rolename = tmp;
4374 +
4375 +               if (!rlast) {
4376 +                       rtmp->prev = NULL;
4377 +                       rolep->transitions = rtmp;
4378 +               } else {
4379 +                       rlast->next = rtmp;
4380 +                       rtmp->prev = rlast;
4381 +               }
4382 +
4383 +               if (!rusertp)
4384 +                       rtmp->next = NULL;
4385 +       }
4386 +
4387 +       return 0;
4388 +}
4389 +
4390 +static struct acl_subject_label *
4391 +do_copy_user_subj(struct acl_subject_label *userp, struct acl_role_label *role)
4392 +{
4393 +       struct acl_subject_label *s_tmp = NULL;
4394 +       unsigned int len;
4395 +       char *tmp;
4396 +       __u32 num_objs;
4397 +       struct acl_ip_label **i_tmp, *i_utmp2;
4398 +       unsigned long i_num;
4399 +       int err;
4400 +
4401 +
4402 +       if ((s_tmp = (struct acl_subject_label *)
4403 +           acl_alloc(sizeof (struct acl_subject_label))) == NULL)
4404 +               return ERR_PTR(-ENOMEM);
4405 +
4406 +       if (copy_from_user(s_tmp, userp,
4407 +                          sizeof (struct acl_subject_label)))
4408 +               return ERR_PTR(-EFAULT);
4409 +
4410 +       if (!s_last) {
4411 +               s_tmp->prev = NULL;
4412 +               role->proc_subject = s_tmp;
4413 +       } else {
4414 +               s_last->next = s_tmp;
4415 +               s_tmp->prev = s_last;
4416 +       }
4417 +
4418 +       s_last = s_tmp;
4419 +
4420 +       len = strnlen_user(s_tmp->filename, PATH_MAX);
4421 +
4422 +       if (!len || len >= PATH_MAX)
4423 +               return ERR_PTR(-EINVAL);
4424 +
4425 +       if ((tmp = (char *) acl_alloc(len)) == NULL)
4426 +               return ERR_PTR(-ENOMEM);
4427 +
4428 +       if (copy_from_user(tmp, s_tmp->filename, len))
4429 +               return ERR_PTR(-EFAULT);
4430 +
4431 +       s_tmp->filename = tmp;
4432 +
4433 +       if (!strcmp(s_tmp->filename, "/"))
4434 +               role->root_label = s_tmp;
4435 +
4436 +       /* set up object hash table */
4437 +       num_objs = count_user_objs(s_tmp->proc_object);
4438 +
4439 +       s_tmp->obj_hash_size = num_objs;
4440 +       s_tmp->obj_hash =
4441 +           (struct acl_object_label **)
4442 +           create_table(&(s_tmp->obj_hash_size));
4443 +
4444 +       if (!s_tmp->obj_hash)
4445 +               return ERR_PTR(-ENOMEM);
4446 +
4447 +       memset(s_tmp->obj_hash, 0,
4448 +              s_tmp->obj_hash_size *
4449 +              sizeof (struct acl_object_label *));
4450 +
4451 +       /* copy before adding in objects, since a nested
4452 +          acl could be found and be the final subject
4453 +          copied
4454 +       */
4455 +
4456 +       s_final = s_tmp;
4457 +
4458 +       /* add in objects */
4459 +       err = copy_user_objs(s_tmp->proc_object, s_tmp, role);
4460 +
4461 +       if (err)
4462 +               return ERR_PTR(err);
4463 +
4464 +       /* add in ip acls */
4465 +
4466 +       if (!s_tmp->ip_num) {
4467 +               s_tmp->ips = NULL;
4468 +               goto insert;
4469 +       }
4470 +
4471 +       i_tmp =
4472 +           (struct acl_ip_label **) acl_alloc(s_tmp->ip_num *
4473 +                                              sizeof (struct
4474 +                                                      acl_ip_label *));
4475 +
4476 +       if (!i_tmp)
4477 +               return ERR_PTR(-ENOMEM);
4478 +
4479 +       for (i_num = 0; i_num < s_tmp->ip_num; i_num++) {
4480 +               *(i_tmp + i_num) =
4481 +                   (struct acl_ip_label *)
4482 +                   acl_alloc(sizeof (struct acl_ip_label));
4483 +               if (!*(i_tmp + i_num))
4484 +                       return ERR_PTR(-ENOMEM);
4485 +
4486 +               if (copy_from_user
4487 +                   (&i_utmp2, s_tmp->ips + i_num,
4488 +                    sizeof (struct acl_ip_label *)))
4489 +                       return ERR_PTR(-EFAULT);
4490 +
4491 +               if (copy_from_user
4492 +                   (*(i_tmp + i_num), i_utmp2,
4493 +                    sizeof (struct acl_ip_label)))
4494 +                       return ERR_PTR(-EFAULT);
4495 +       }
4496 +
4497 +       s_tmp->ips = i_tmp;
4498 +
4499 +insert:
4500 +       if (!insert_name_entry(s_tmp->filename, s_tmp->inode,
4501 +                              s_tmp->device))
4502 +               return ERR_PTR(-ENOMEM);
4503 +
4504 +       return s_tmp;
4505 +}
4506 +
4507 +static int
4508 +copy_user_subjs(struct acl_subject_label *userp, struct acl_role_label *role)
4509 +{
4510 +       struct acl_subject_label s_pre;
4511 +       struct acl_subject_label * ret;
4512 +       int err;
4513 +
4514 +       while (userp) {
4515 +               if (copy_from_user(&s_pre, userp,
4516 +                                  sizeof (struct acl_subject_label)))
4517 +                       return -EFAULT;
4518 +               
4519 +               /* do not add nested subjects here, add
4520 +                  while parsing objects
4521 +               */
4522 +
4523 +               if (s_pre.parent_subject) {
4524 +                       userp = s_pre.prev;
4525 +                       continue;
4526 +               }
4527 +
4528 +               ret = do_copy_user_subj(userp, role);
4529 +
4530 +               err = PTR_ERR(ret);
4531 +               if (IS_ERR(ret))
4532 +                       return err;
4533 +
4534 +               insert_acl_subj_label(ret, role);
4535 +
4536 +               userp = s_pre.prev;
4537 +       }
4538 +
4539 +       s_final->next = NULL;
4540 +
4541 +       return 0;
4542 +}
4543 +
4544 +static int
4545 +copy_user_acl(struct gr_arg *arg)
4546 +{
4547 +       struct acl_role_label *r_tmp = NULL, **r_utmp, *r_utmp2, *r_last;
4548 +       struct sprole_pw *sptmp;
4549 +       unsigned long r_num;
4550 +       unsigned int len;
4551 +       char *tmp;
4552 +       int err = 0;
4553 +       __u16 i;
4554 +       __u32 num_subjs;
4555 +
4556 +       /* we need a default and kernel role */
4557 +       if (arg->role_db.r_entries < 2)
4558 +               return -EINVAL;
4559 +
4560 +       /* copy special role authentication info from userspace */
4561 +
4562 +       num_sprole_pws = arg->num_sprole_pws;
4563 +       acl_special_roles = (struct sprole_pw **) acl_alloc(num_sprole_pws * sizeof(struct sprole_pw *));
4564 +
4565 +       if (!acl_special_roles) {
4566 +               err = -ENOMEM;
4567 +               goto cleanup;
4568 +       }
4569 +
4570 +       for (i = 0; i < num_sprole_pws; i++) {
4571 +               sptmp = (struct sprole_pw *) acl_alloc(sizeof(struct sprole_pw));
4572 +               if (!sptmp) {
4573 +                       err = -ENOMEM;
4574 +                       goto cleanup;
4575 +               }
4576 +               if (copy_from_user(sptmp, arg->sprole_pws + i,
4577 +                                  sizeof (struct sprole_pw))) {
4578 +                       err = -EFAULT;
4579 +                       goto cleanup;
4580 +               }
4581 +
4582 +               len =
4583 +                   strnlen_user(sptmp->rolename, GR_SPROLE_LEN);
4584 +
4585 +               if (!len || len >= GR_SPROLE_LEN) {
4586 +                       err = -EINVAL;
4587 +                       goto cleanup;
4588 +               }
4589 +
4590 +               if ((tmp = (char *) acl_alloc(len)) == NULL) {
4591 +                       err = -ENOMEM;
4592 +                       goto cleanup;
4593 +               }
4594 +
4595 +               if (copy_from_user(tmp, sptmp->rolename, len)) {
4596 +                       err = -EFAULT;
4597 +                       goto cleanup;
4598 +               }
4599 +
4600 +#ifdef CONFIG_GRKERNSEC_ACL_DEBUG
4601 +               printk(KERN_ALERT "Copying special role %s\n", tmp);
4602 +#endif
4603 +               sptmp->rolename = tmp;
4604 +               acl_special_roles[i] = sptmp;
4605 +       }
4606 +
4607 +       r_utmp = (struct acl_role_label **) arg->role_db.r_table;
4608 +
4609 +       for (r_num = 0; r_num < arg->role_db.r_entries; r_num++) {
4610 +               r_last = r_tmp;
4611 +
4612 +               r_tmp = acl_alloc(sizeof (struct acl_role_label));
4613 +
4614 +               if (!r_tmp) {
4615 +                       err = -ENOMEM;
4616 +                       goto cleanup;
4617 +               }
4618 +
4619 +               if (copy_from_user(&r_utmp2, r_utmp + r_num,
4620 +                                  sizeof (struct acl_role_label *))) {
4621 +                       err = -EFAULT;
4622 +                       goto cleanup;
4623 +               }
4624 +
4625 +               if (copy_from_user(r_tmp, r_utmp2,
4626 +                                  sizeof (struct acl_role_label))) {
4627 +                       err = -EFAULT;
4628 +                       goto cleanup;
4629 +               }
4630 +
4631 +               if (!r_last) {
4632 +                       r_tmp->prev = NULL;
4633 +                       role_list_head = r_tmp;
4634 +               } else {
4635 +                       r_last->next = r_tmp;
4636 +                       r_tmp->prev = r_last;
4637 +               }
4638 +
4639 +               if (r_num == (arg->role_db.r_entries - 1))
4640 +                       r_tmp->next = NULL;
4641 +
4642 +               len = strnlen_user(r_tmp->rolename, PATH_MAX);
4643 +
4644 +               if (!len || len >= PATH_MAX) {
4645 +                       err = -EINVAL;
4646 +                       goto cleanup;
4647 +               }
4648 +
4649 +               if ((tmp = (char *) acl_alloc(len)) == NULL) {
4650 +                       err = -ENOMEM;
4651 +                       goto cleanup;
4652 +               }
4653 +               if (copy_from_user(tmp, r_tmp->rolename, len)) {
4654 +                       err = -EFAULT;
4655 +                       goto cleanup;
4656 +               }
4657 +               r_tmp->rolename = tmp;
4658 +
4659 +               if (!strcmp(r_tmp->rolename, "default")
4660 +                   && (r_tmp->roletype & GR_ROLE_DEFAULT)) {
4661 +                       default_role = r_tmp;
4662 +               } else if (!strcmp(r_tmp->rolename, ":::kernel:::")) {
4663 +                       kernel_role = r_tmp;
4664 +               }
4665 +
4666 +               num_subjs = count_user_subjs(r_tmp->proc_subject);
4667 +
4668 +               r_tmp->subj_hash_size = num_subjs;
4669 +               r_tmp->subj_hash =
4670 +                   (struct acl_subject_label **)
4671 +                   create_table(&(r_tmp->subj_hash_size));
4672 +
4673 +               if (!r_tmp->subj_hash) {
4674 +                       err = -ENOMEM;
4675 +                       goto cleanup;
4676 +               }
4677 +
4678 +               err = copy_user_allowedips(r_tmp);
4679 +               if (err)
4680 +                       goto cleanup;
4681 +
4682 +               err = copy_user_transitions(r_tmp);
4683 +               if (err)
4684 +                       goto cleanup;
4685 +
4686 +               memset(r_tmp->subj_hash, 0,
4687 +                      r_tmp->subj_hash_size *
4688 +                      sizeof (struct acl_subject_label *));
4689 +
4690 +               s_last = NULL;
4691 +
4692 +               err = copy_user_subjs(r_tmp->proc_subject, r_tmp);
4693 +
4694 +               if (err)
4695 +                       goto cleanup;
4696 +
4697 +               insert_acl_role_label(r_tmp);
4698 +       }
4699 +
4700 +       goto return_err;
4701 +      cleanup:
4702 +       free_variables();
4703 +      return_err:
4704 +       return err;
4705 +
4706 +}
4707 +
4708 +static int
4709 +gracl_init(struct gr_arg *args)
4710 +{
4711 +       int error = 0;
4712 +
4713 +       memcpy(&system_salt, args->salt, sizeof (system_salt));
4714 +       memcpy(&system_sum, args->sum, sizeof (system_sum));
4715 +
4716 +       if (init_variables(args->role_db.o_entries, args->role_db.s_entries,
4717 +                          args->role_db.i_entries, args->role_db.r_entries,
4718 +                          args->role_db.a_entries, args->role_db.t_entries,
4719 +                          args->num_sprole_pws)) {
4720 +               security_alert_good(GR_INITF_ACL_MSG, GR_VERSION);
4721 +               error = -ENOMEM;
4722 +               free_variables();
4723 +               goto out;
4724 +       }
4725 +
4726 +       error = copy_user_acl(args);
4727 +       if (error)
4728 +               goto out;
4729 +
4730 +       if ((error = gr_set_acls(0))) {
4731 +               free_variables();
4732 +               goto out;
4733 +       }
4734 +
4735 +       gr_status |= GR_READY;
4736 +      out:
4737 +       return error;
4738 +}
4739 +
4740 +static struct acl_object_label *
4741 +chk_obj_label(const struct dentry *l_dentry, const struct vfsmount *l_mnt,
4742 +             const struct acl_subject_label *subj)
4743 +{
4744 +       struct dentry *dentry = (struct dentry *) l_dentry;
4745 +       struct vfsmount *mnt = (struct vfsmount *) l_mnt;
4746 +       struct dentry *root;
4747 +       struct vfsmount *rootmnt;
4748 +       struct acl_object_label *retval;
4749 +
4750 +       read_lock(&child_reaper->fs->lock);
4751 +       rootmnt = mntget(child_reaper->fs->rootmnt);
4752 +       root = dget(child_reaper->fs->root);
4753 +       read_unlock(&child_reaper->fs->lock);
4754 +       spin_lock(&dcache_lock);
4755 +
4756 +       for (;;) {
4757 +               if (unlikely(dentry == root && mnt == rootmnt))
4758 +                       break;
4759 +               if (unlikely(dentry == mnt->mnt_root || IS_ROOT(dentry))) {
4760 +                       if (mnt->mnt_parent == mnt)
4761 +                               break;
4762 +
4763 +                       read_lock(&gr_inode_lock);
4764 +                       retval =
4765 +                           lookup_acl_obj_label(dentry->d_inode->i_ino,
4766 +                                                dentry->d_inode->i_dev, subj);
4767 +                       read_unlock(&gr_inode_lock);
4768 +                       if (unlikely(retval != NULL))
4769 +                               goto out;
4770 +
4771 +                       dentry = mnt->mnt_mountpoint;
4772 +                       mnt = mnt->mnt_parent;
4773 +                       continue;
4774 +               }
4775 +
4776 +               read_lock(&gr_inode_lock);
4777 +               retval =
4778 +                   lookup_acl_obj_label(dentry->d_inode->i_ino,
4779 +                                        dentry->d_inode->i_dev, subj);
4780 +               read_unlock(&gr_inode_lock);
4781 +               if (unlikely(retval != NULL))
4782 +                       goto out;
4783 +
4784 +               dentry = dentry->d_parent;
4785 +       }
4786 +
4787 +       read_lock(&gr_inode_lock);
4788 +       retval =
4789 +           lookup_acl_obj_label(dentry->d_inode->i_ino, dentry->d_inode->i_dev,
4790 +                                subj);
4791 +       read_unlock(&gr_inode_lock);
4792 +
4793 +       if (unlikely(retval == NULL)) {
4794 +               read_lock(&gr_inode_lock);
4795 +               retval =
4796 +                   lookup_acl_obj_label(root->d_inode->i_ino,
4797 +                                        root->d_inode->i_dev, subj);
4798 +               read_unlock(&gr_inode_lock);
4799 +       }
4800 +      out:
4801 +       spin_unlock(&dcache_lock);
4802 +       dput(root);
4803 +       mntput(rootmnt);
4804 +
4805 +       return retval;
4806 +}
4807 +
4808 +static struct acl_subject_label *
4809 +chk_subj_label(const struct dentry *l_dentry, const struct vfsmount *l_mnt,
4810 +              const struct acl_role_label *role)
4811 +{
4812 +       struct dentry *dentry = (struct dentry *) l_dentry;
4813 +       struct vfsmount *mnt = (struct vfsmount *) l_mnt;
4814 +       struct dentry *root;
4815 +       struct vfsmount *rootmnt;
4816 +       struct acl_subject_label *retval;
4817 +
4818 +       read_lock(&child_reaper->fs->lock);
4819 +       rootmnt = mntget(child_reaper->fs->rootmnt);
4820 +       root = dget(child_reaper->fs->root);
4821 +       read_unlock(&child_reaper->fs->lock);
4822 +       spin_lock(&dcache_lock);
4823 +
4824 +       for (;;) {
4825 +               if (unlikely(dentry == root && mnt == rootmnt))
4826 +                       break;
4827 +               if (unlikely(dentry == mnt->mnt_root || IS_ROOT(dentry))) {
4828 +                       if (mnt->mnt_parent == mnt)
4829 +                               break;
4830 +
4831 +                       read_lock(&gr_inode_lock);
4832 +                       retval =
4833 +                           lookup_acl_subj_label(dentry->d_inode->i_ino,
4834 +                                                 dentry->d_inode->i_dev, role);
4835 +                       read_unlock(&gr_inode_lock);
4836 +                       if (unlikely(retval != NULL))
4837 +                               goto out;
4838 +
4839 +                       dentry = mnt->mnt_mountpoint;
4840 +                       mnt = mnt->mnt_parent;
4841 +                       continue;
4842 +               }
4843 +
4844 +               read_lock(&gr_inode_lock);
4845 +               retval =
4846 +                   lookup_acl_subj_label(dentry->d_inode->i_ino,
4847 +                                         dentry->d_inode->i_dev, role);
4848 +               read_unlock(&gr_inode_lock);
4849 +               if (unlikely(retval != NULL))
4850 +                       goto out;
4851 +
4852 +               dentry = dentry->d_parent;
4853 +       }
4854 +
4855 +       read_lock(&gr_inode_lock);
4856 +       retval =
4857 +           lookup_acl_subj_label(dentry->d_inode->i_ino,
4858 +                                 dentry->d_inode->i_dev, role);
4859 +       read_unlock(&gr_inode_lock);
4860 +
4861 +       if (unlikely(retval == NULL)) {
4862 +               read_lock(&gr_inode_lock);
4863 +               retval =
4864 +                   lookup_acl_subj_label(root->d_inode->i_ino,
4865 +                                         root->d_inode->i_dev, role);
4866 +               read_unlock(&gr_inode_lock);
4867 +       }
4868 +      out:
4869 +       spin_unlock(&dcache_lock);
4870 +       dput(root);
4871 +       mntput(rootmnt);
4872 +
4873 +       return retval;
4874 +}
4875 +
4876 +static __inline__ void
4877 +gr_log_learn(const struct acl_role_label *role, const uid_t uid, const gid_t gid,
4878 +            const struct task_struct *task, const char *pathname,
4879 +            const __u32 mode)
4880 +{
4881 +       security_learn(GR_LEARN_AUDIT_MSG, role->rolename, role->roletype,
4882 +                      uid, gid, task->exec_file ? gr_to_filename1(task->exec_file->f_dentry,
4883 +                      task->exec_file->f_vfsmnt) : task->acl->filename, task->acl->filename,
4884 +                      1, 1, pathname, (unsigned long) mode, NIPQUAD(task->curr_ip));
4885 +
4886 +       return;
4887 +}
4888 +
4889 +__u32
4890 +gr_check_link(const struct dentry * new_dentry,
4891 +             const struct dentry * parent_dentry,
4892 +             const struct vfsmount * parent_mnt,
4893 +             const struct dentry * old_dentry, const struct vfsmount * old_mnt)
4894 +{
4895 +       struct acl_object_label *obj;
4896 +       __u32 oldmode, newmode;
4897 +
4898 +       if (unlikely(!(gr_status & GR_READY)))
4899 +               return (GR_WRITE | GR_CREATE);
4900 +
4901 +       obj = chk_obj_label(old_dentry, old_mnt, current->acl);
4902 +       oldmode = obj->mode;
4903 +
4904 +       if (current->acl->mode & GR_LEARN)
4905 +               oldmode |= (GR_WRITE | GR_CREATE);
4906 +       newmode =
4907 +           gr_check_create(new_dentry, parent_dentry, parent_mnt,
4908 +                           oldmode | GR_CREATE | GR_AUDIT_CREATE |
4909 +                           GR_AUDIT_WRITE | GR_SUPPRESS);
4910 +
4911 +       if ((newmode & oldmode) == oldmode)
4912 +               return newmode;
4913 +       else if (current->acl->mode & GR_LEARN) {
4914 +               gr_log_learn(current->role, current->uid, current->gid,
4915 +                       current, gr_to_filename(old_dentry, old_mnt), oldmode);
4916 +               return (GR_WRITE | GR_CREATE);
4917 +       } else if (newmode & GR_SUPPRESS)
4918 +               return GR_SUPPRESS;
4919 +       else
4920 +               return 0;
4921 +}
4922 +
4923 +__u32
4924 +gr_search_file(const struct dentry * dentry, const __u32 mode,
4925 +              const struct vfsmount * mnt)
4926 +{
4927 +       __u32 retval = mode;
4928 +       struct acl_subject_label *curracl;
4929 +       struct acl_object_label *currobj;
4930 +
4931 +       if (unlikely(!(gr_status & GR_READY)))
4932 +               return (mode & ~GR_AUDITS);
4933 +
4934 +       curracl = current->acl;
4935 +
4936 +       currobj = chk_obj_label(dentry, mnt, curracl);
4937 +       retval = currobj->mode & mode;
4938 +
4939 +       if (unlikely
4940 +           ((curracl->mode & GR_LEARN) && (mode != GR_PTRACERD)
4941 +            && (retval != (mode & ~(GR_AUDITS | GR_SUPPRESS))))) {
4942 +               __u32 new_mode = mode;
4943 +
4944 +               new_mode &= ~(GR_AUDITS | GR_SUPPRESS);
4945 +
4946 +               retval = new_mode;
4947 +
4948 +               if (!(mode & GR_NOLEARN))
4949 +                       gr_log_learn(current->role, current->uid, current->gid,
4950 +                                    current, gr_to_filename(dentry, mnt), new_mode);
4951 +       }
4952 +
4953 +       return retval;
4954 +}
4955 +
4956 +__u32
4957 +gr_check_create(const struct dentry * new_dentry, const struct dentry * parent,
4958 +               const struct vfsmount * mnt, const __u32 mode)
4959 +{
4960 +       struct name_entry *match;
4961 +       struct acl_object_label *matchpo;
4962 +       struct acl_subject_label *curracl;
4963 +       __u32 retval;
4964 +
4965 +       if (unlikely(!(gr_status & GR_READY)))
4966 +               return (mode & ~GR_AUDITS);
4967 +
4968 +       match = lookup_name_entry(gr_to_filename(new_dentry, mnt));
4969 +
4970 +       if (!match)
4971 +               goto check_parent;
4972 +
4973 +       curracl = current->acl;
4974 +
4975 +       read_lock(&gr_inode_lock);
4976 +       matchpo = lookup_acl_obj_label_create(match->inode, match->device, curracl);
4977 +       read_unlock(&gr_inode_lock);
4978 +
4979 +       if (matchpo) {
4980 +               if ((matchpo->mode & mode) !=
4981 +                   (mode & ~(GR_AUDITS | GR_SUPPRESS))
4982 +                   && curracl->mode & GR_LEARN) {
4983 +                       __u32 new_mode = mode;
4984 +
4985 +                       new_mode &= ~(GR_AUDITS | GR_SUPPRESS);
4986 +
4987 +                       gr_log_learn(current->role, current->uid, current->gid,
4988 +                                    current, gr_to_filename(new_dentry, mnt), new_mode);
4989 +
4990 +                       return new_mode;
4991 +               }
4992 +               return (matchpo->mode & mode);
4993 +       }
4994 +
4995 +      check_parent:
4996 +       curracl = current->acl;
4997 +
4998 +       matchpo = chk_obj_label(parent, mnt, curracl);
4999 +       retval = matchpo->mode & mode;
5000 +
5001 +       if ((retval != (mode & ~(GR_AUDITS | GR_SUPPRESS)))
5002 +           && (curracl->mode & GR_LEARN)) {
5003 +               __u32 new_mode = mode;
5004 +
5005 +               new_mode &= ~(GR_AUDITS | GR_SUPPRESS);
5006 +
5007 +               gr_log_learn(current->role, current->uid, current->gid, 
5008 +                            current, gr_to_filename(new_dentry, mnt), new_mode);
5009 +               return new_mode;
5010 +       }
5011 +
5012 +       return retval;
5013 +}
5014 +
5015 +int
5016 +gr_check_hidden_task(const struct task_struct *task)
5017 +{
5018 +       if (unlikely(!(gr_status & GR_READY)))
5019 +               return 0;
5020 +
5021 +       if (!(task->acl->mode & GR_FIND) && !(current->acl->mode & GR_VIEW))
5022 +               return 1;
5023 +
5024 +       return 0;
5025 +}
5026 +
5027 +int
5028 +gr_check_protected_task(const struct task_struct *task)
5029 +{
5030 +       if (unlikely(!(gr_status & GR_READY) || !task))
5031 +               return 0;
5032 +
5033 +       if ((task->acl->mode & GR_PROTECTED) && !(current->acl->mode & GR_KILL))
5034 +               return 1;
5035 +
5036 +       return 0;
5037 +}
5038 +
5039 +__inline__ void
5040 +gr_copy_label(struct task_struct *tsk)
5041 +{
5042 +       tsk->used_accept = 0;
5043 +       tsk->used_connect = 0;
5044 +       tsk->acl_sp_role = 0;
5045 +       tsk->acl_role_id = current->acl_role_id;
5046 +       tsk->acl = current->acl;
5047 +       tsk->role = current->role;
5048 +       tsk->curr_ip = current->curr_ip;
5049 +       if (current->exec_file)
5050 +               get_file(current->exec_file);
5051 +       tsk->exec_file = current->exec_file;
5052 +       tsk->is_writable = current->is_writable;
5053 +       if (unlikely(current->used_accept))
5054 +               current->curr_ip = 0;
5055 +
5056 +       return;
5057 +}
5058 +
5059 +static __inline__ void
5060 +gr_set_proc_res(void)
5061 +{
5062 +       struct acl_subject_label *proc;
5063 +       unsigned short i;
5064 +
5065 +       proc = current->acl;
5066 +
5067 +       if (proc->mode & GR_LEARN)
5068 +               return;
5069 +
5070 +       for (i = 0; i < RLIM_NLIMITS; i++) {
5071 +               if (!(proc->resmask & (1 << i)))
5072 +                       continue;
5073 +
5074 +               current->rlim[i].rlim_cur = proc->res[i].rlim_cur;
5075 +               current->rlim[i].rlim_max = proc->res[i].rlim_max;
5076 +       }
5077 +
5078 +       return;
5079 +}
5080 +
5081 +static __inline__ void
5082 +do_set_role_label(struct task_struct *task, const uid_t uid, const gid_t gid)
5083 +{
5084 +       task->role = lookup_acl_role_label(task, uid, gid);
5085 +
5086 +       return;
5087 +}
5088 +
5089 +void
5090 +gr_set_role_label(struct task_struct *task, const uid_t uid, const uid_t gid)
5091 +{
5092 +       struct acl_object_label *obj;
5093 +       struct file *filp;
5094 +
5095 +       if (unlikely(!(gr_status & GR_READY)))
5096 +               return;
5097 +
5098 +       filp = task->exec_file;
5099 +
5100 +       /* kernel process, we'll give them the kernel role */
5101 +       if (unlikely(!filp)) {
5102 +               task->role = kernel_role;
5103 +               task->acl = kernel_role->root_label;
5104 +               return;
5105 +       } else if (!task->role || !(task->role->roletype & GR_ROLE_SPECIAL))
5106 +               do_set_role_label(task, uid, gid);
5107 +
5108 +       task->acl =
5109 +           chk_subj_label(filp->f_dentry, filp->f_vfsmnt, task->role);
5110 +
5111 +       task->is_writable = 0;
5112 +
5113 +       /* ignore additional mmap checks for processes that are writable 
5114 +          by the default ACL */
5115 +       obj = chk_obj_label(filp->f_dentry, filp->f_vfsmnt, default_role->root_label);
5116 +       if (unlikely(obj->mode & GR_WRITE))
5117 +               task->is_writable = 1;
5118 +       obj = chk_obj_label(filp->f_dentry, filp->f_vfsmnt, task->role->root_label);
5119 +       if (unlikely(obj->mode & GR_WRITE))
5120 +               task->is_writable = 1;
5121 +
5122 +#ifdef CONFIG_GRKERNSEC_ACL_DEBUG
5123 +       printk(KERN_ALERT "Set role label for (%s:%d): role:%s, subject:%s\n", task->comm, task->pid, task->role->rolename, task->acl->filename);
5124 +#endif
5125 +
5126 +       gr_set_proc_res();
5127 +
5128 +       return;
5129 +}
5130 +
5131 +void
5132 +gr_set_proc_label(const struct dentry *dentry, const struct vfsmount *mnt)
5133 +{
5134 +       struct acl_subject_label *newacl;
5135 +       struct acl_object_label *obj;
5136 +       __u32 retmode;
5137 +
5138 +       if (unlikely(!(gr_status & GR_READY)))
5139 +               return;
5140 +
5141 +       newacl = chk_subj_label(dentry, mnt, current->role);
5142 +
5143 +       obj = chk_obj_label(dentry, mnt, current->acl);
5144 +       retmode = obj->mode & (GR_INHERIT | GR_AUDIT_INHERIT);
5145 +
5146 +       if ((newacl->mode & GR_LEARN) || !(retmode & GR_INHERIT)) {
5147 +               if (obj->nested)
5148 +                       current->acl = obj->nested;
5149 +               else
5150 +                       current->acl = newacl;
5151 +       } else if (retmode & GR_INHERIT && retmode & GR_AUDIT_INHERIT)
5152 +               security_audit(GR_INHERIT_ACL_MSG, current->acl->filename,
5153 +                              gr_to_filename(dentry, mnt), DEFAULTSECARGS);
5154 +
5155 +       current->is_writable = 0;
5156 +
5157 +       /* ignore additional mmap checks for processes that are writable 
5158 +          by the default ACL */
5159 +       obj = chk_obj_label(dentry, mnt, default_role->root_label);
5160 +       if (unlikely(obj->mode & GR_WRITE))
5161 +               current->is_writable = 1;
5162 +       obj = chk_obj_label(dentry, mnt, current->role->root_label);
5163 +       if (unlikely(obj->mode & GR_WRITE))
5164 +               current->is_writable = 1;
5165 +
5166 +       gr_set_proc_res();
5167 +
5168 +#ifdef CONFIG_GRKERNSEC_ACL_DEBUG
5169 +       printk(KERN_ALERT "Set subject label for (%s:%d): role:%s, subject:%s\n", current->comm, current->pid, current->role->rolename, current->acl->filename);
5170 +#endif
5171 +       return;
5172 +}
5173 +
5174 +static __inline__ void
5175 +do_handle_delete(const ino_t ino, const kdev_t dev)
5176 +{
5177 +       struct acl_object_label *matchpo;
5178 +       struct acl_subject_label *matchps;
5179 +       struct acl_subject_label *i;
5180 +       struct acl_role_label *role;
5181 +
5182 +       for (role = role_list_head; role; role = role->next) {
5183 +               for (i = role->proc_subject; i; i = i->next) {
5184 +                       if (unlikely(i->parent_subject &&
5185 +                                    (i->inode == ino) &&
5186 +                                    (i->device == dev)))
5187 +                               i->mode |= GR_DELETED;
5188 +                       if (unlikely((matchpo =
5189 +                            lookup_acl_obj_label(ino, dev, i)) != NULL))
5190 +                               matchpo->mode |= GR_DELETED;
5191 +               }
5192 +
5193 +               if (unlikely((matchps = lookup_acl_subj_label(ino, dev, role)) != NULL))
5194 +                       matchps->mode |= GR_DELETED;
5195 +       }
5196 +
5197 +       return;
5198 +}
5199 +
5200 +void
5201 +gr_handle_delete(const ino_t ino, const kdev_t dev)
5202 +{
5203 +       if (unlikely(!(gr_status & GR_READY)))
5204 +               return;
5205 +
5206 +       write_lock(&gr_inode_lock);
5207 +       if (unlikely((unsigned long)lookup_inodev_entry(ino, dev)))
5208 +               do_handle_delete(ino, dev);
5209 +       write_unlock(&gr_inode_lock);
5210 +
5211 +       return;
5212 +}
5213 +
5214 +static __inline__ void
5215 +update_acl_obj_label(const ino_t oldinode, const kdev_t olddevice,
5216 +                    const ino_t newinode, const kdev_t newdevice,
5217 +                    struct acl_subject_label *subj)
5218 +{
5219 +       unsigned long index = fhash(oldinode, olddevice, subj->obj_hash_size);
5220 +       struct acl_object_label **match;
5221 +       struct acl_object_label *tmp;
5222 +       __u8 i = 0;
5223 +
5224 +       match = &subj->obj_hash[index];
5225 +
5226 +       while (*match && ((*match)->inode != oldinode ||
5227 +              (*match)->device != olddevice ||
5228 +              !((*match)->mode & GR_DELETED))) {
5229 +               index = (index + (1 << i)) % subj->obj_hash_size;
5230 +               match = &subj->obj_hash[index];
5231 +               i = (i + 1) % 32;
5232 +       }
5233 +
5234 +       if (*match && ((*match) != deleted_object)
5235 +           && ((*match)->inode == oldinode)
5236 +           && ((*match)->device == olddevice)
5237 +           && ((*match)->mode & GR_DELETED)) {
5238 +               tmp = *match;
5239 +               tmp->inode = newinode;
5240 +               tmp->device = newdevice;
5241 +               tmp->mode &= ~GR_DELETED;
5242 +
5243 +               *match = deleted_object;
5244 +
5245 +               insert_acl_obj_label(tmp, subj);
5246 +       }
5247 +
5248 +       return;
5249 +}
5250 +
5251 +static __inline__ void
5252 +update_acl_subj_label(const ino_t oldinode, const kdev_t olddevice,
5253 +                     const ino_t newinode, const kdev_t newdevice,
5254 +                     struct acl_role_label *role)
5255 +{
5256 +       struct acl_subject_label **s_hash = role->subj_hash;
5257 +       unsigned long subj_size = role->subj_hash_size;
5258 +       unsigned long index = fhash(oldinode, olddevice, subj_size);
5259 +       struct acl_subject_label **match;
5260 +       struct acl_subject_label *tmp;
5261 +       __u8 i = 0;
5262 +
5263 +       match = &s_hash[index];
5264 +
5265 +       while (*match && ((*match)->inode != oldinode ||
5266 +              (*match)->device != olddevice ||
5267 +              !((*match)->mode & GR_DELETED))) {
5268 +               index = (index + (1 << i)) % subj_size;
5269 +               i = (i + 1) % 32;
5270 +               match = &s_hash[index];
5271 +       }
5272 +
5273 +       if (*match && (*match != deleted_subject)
5274 +           && ((*match)->inode == oldinode)
5275 +           && ((*match)->device == olddevice)
5276 +           && ((*match)->mode & GR_DELETED)) {
5277 +               tmp = *match;
5278 +
5279 +               tmp->inode = newinode;
5280 +               tmp->device = newdevice;
5281 +               tmp->mode &= ~GR_DELETED;
5282 +
5283 +               *match = deleted_subject;
5284 +
5285 +               insert_acl_subj_label(tmp, role);
5286 +       }
5287 +
5288 +       return;
5289 +}
5290 +
5291 +static __inline__ void
5292 +update_inodev_entry(const ino_t oldinode, const kdev_t olddevice,
5293 +                   const ino_t newinode, const kdev_t newdevice)
5294 +{
5295 +       unsigned long index = fhash(oldinode, olddevice, inodev_set.n_size);
5296 +       struct name_entry **match;
5297 +       struct name_entry *tmp;
5298 +       __u8 i = 0;
5299 +
5300 +       match = &inodev_set.n_hash[index];
5301 +
5302 +       while (*match
5303 +              && ((*match)->inode != oldinode
5304 +                  || (*match)->device != olddevice)) {
5305 +               index = (index + (1 << i)) % inodev_set.n_size;
5306 +               i = (i + 1) % 32;
5307 +               match = &inodev_set.n_hash[index];
5308 +       }
5309 +
5310 +       if (*match && (*match != deleted_inodev)
5311 +           && ((*match)->inode == oldinode)
5312 +           && ((*match)->device == olddevice)) {
5313 +               tmp = *match;
5314 +
5315 +               tmp->inode = newinode;
5316 +               tmp->device = newdevice;
5317 +
5318 +               *match = deleted_inodev;
5319 +
5320 +               insert_inodev_entry(tmp);
5321 +       }
5322 +
5323 +       return;
5324 +}
5325 +
5326 +static __inline__ void
5327 +do_handle_create(const struct name_entry *matchn, const struct dentry *dentry,
5328 +                const struct vfsmount *mnt)
5329 +{
5330 +       struct acl_subject_label *i;
5331 +       struct acl_role_label *role;
5332 +
5333 +       for (role = role_list_head; role; role = role->next) {
5334 +               update_acl_subj_label(matchn->inode, matchn->device,
5335 +                                     dentry->d_inode->i_ino,
5336 +                                     dentry->d_inode->i_dev, role);
5337 +
5338 +               for (i = role->proc_subject; i; i = i->next) {
5339 +                       if (unlikely(i->parent_subject &&
5340 +                                    (i->inode == dentry->d_inode->i_ino) &&
5341 +                                    (i->device == dentry->d_inode->i_dev))) {
5342 +                               i->inode = dentry->d_inode->i_ino;
5343 +                               i->device = dentry->d_inode->i_dev;
5344 +                       }
5345 +                       update_acl_obj_label(matchn->inode, matchn->device,
5346 +                                            dentry->d_inode->i_ino,
5347 +                                            dentry->d_inode->i_dev, i);
5348 +               }
5349 +       }
5350 +
5351 +       update_inodev_entry(matchn->inode, matchn->device,
5352 +                           dentry->d_inode->i_ino, dentry->d_inode->i_dev);
5353 +
5354 +       return;
5355 +}
5356 +
5357 +void
5358 +gr_handle_create(const struct dentry *dentry, const struct vfsmount *mnt)
5359 +{
5360 +       struct name_entry *matchn;
5361 +
5362 +       if (unlikely(!(gr_status & GR_READY)))
5363 +               return;
5364 +
5365 +       matchn = lookup_name_entry(gr_to_filename(dentry, mnt));
5366 +
5367 +       if (unlikely((unsigned long)matchn)) {
5368 +               write_lock(&gr_inode_lock);
5369 +               do_handle_create(matchn, dentry, mnt);
5370 +               write_unlock(&gr_inode_lock);
5371 +       }
5372 +
5373 +       return;
5374 +}
5375 +
5376 +int
5377 +gr_handle_rename(struct inode *old_dir, struct inode *new_dir,
5378 +                struct dentry *old_dentry,
5379 +                struct dentry *new_dentry,
5380 +                struct vfsmount *mnt, const __u8 replace)
5381 +{
5382 +       struct name_entry *matchn;
5383 +       int error = 0;
5384 +
5385 +       matchn = lookup_name_entry(gr_to_filename(new_dentry, mnt));
5386 +
5387 +       lock_kernel();
5388 +       error = vfs_rename(old_dir, old_dentry, new_dir, new_dentry);
5389 +       unlock_kernel();
5390 +
5391 +       if (unlikely(error))
5392 +               return error;
5393 +
5394 +       /* we wouldn't have to check d_inode if it weren't for
5395 +          NFS silly-renaming
5396 +        */
5397 +
5398 +       write_lock(&gr_inode_lock);
5399 +       if (unlikely(replace && new_dentry->d_inode)) {
5400 +               if (unlikely(lookup_inodev_entry(new_dentry->d_inode->i_ino,
5401 +                                       new_dentry->d_inode->i_dev) &&
5402 +                   (old_dentry->d_inode->i_nlink <= 1)))
5403 +                       do_handle_delete(new_dentry->d_inode->i_ino,
5404 +                                        new_dentry->d_inode->i_dev);
5405 +       }
5406 +
5407 +       if (unlikely(lookup_inodev_entry(old_dentry->d_inode->i_ino,
5408 +                               old_dentry->d_inode->i_dev) &&
5409 +           (old_dentry->d_inode->i_nlink <= 1)))
5410 +               do_handle_delete(old_dentry->d_inode->i_ino,
5411 +                                old_dentry->d_inode->i_dev);
5412 +
5413 +       if (unlikely((unsigned long)matchn))
5414 +               do_handle_create(matchn, old_dentry, mnt);
5415 +       write_unlock(&gr_inode_lock);
5416 +
5417 +       return error;
5418 +}
5419 +
5420 +static int
5421 +lookup_special_role_auth(const char *rolename, unsigned char **salt,
5422 +                        unsigned char **sum)
5423 +{
5424 +       struct acl_role_label *r;
5425 +       struct role_transition *trans;
5426 +       __u16 i;
5427 +       int found = 0;
5428 +
5429 +       /* check transition table */
5430 +
5431 +       for (trans = current->role->transitions; trans; trans = trans->next) {
5432 +               if (!strcmp(rolename, trans->rolename)) {
5433 +                       found = 1;
5434 +                       break;
5435 +               }
5436 +       }
5437 +
5438 +       if (!found)
5439 +               return 0;
5440 +
5441 +       /* handle special roles that do not require authentication */
5442 +
5443 +       for (r = role_list_head; r; r = r->next) {
5444 +               if (!strcmp(rolename, r->rolename)
5445 +                   && (r->roletype & GR_ROLE_NOPW)) {
5446 +                       *salt = NULL;
5447 +                       *sum = NULL;
5448 +                       return 1;
5449 +               }
5450 +       }
5451 +
5452 +       for (i = 0; i < num_sprole_pws; i++) {
5453 +               if (!strcmp(rolename, acl_special_roles[i]->rolename)) {
5454 +                       *salt = acl_special_roles[i]->salt;
5455 +                       *sum = acl_special_roles[i]->sum;
5456 +                       return 1;
5457 +               }
5458 +       }
5459 +
5460 +       return 0;
5461 +}
5462 +
5463 +static void
5464 +assign_special_role(char *rolename)
5465 +{
5466 +       struct acl_object_label *obj;
5467 +       struct acl_role_label *r;
5468 +       struct acl_role_label *assigned = NULL;
5469 +       struct task_struct *tsk;
5470 +       struct file *filp;
5471 +
5472 +       for (r = role_list_head; r; r = r->next)
5473 +               if (!strcmp(rolename, r->rolename) &&
5474 +                   (r->roletype & GR_ROLE_SPECIAL))
5475 +                       assigned = r;
5476 +
5477 +       if (!assigned)
5478 +               return;
5479 +
5480 +       tsk = current->p_pptr;
5481 +       filp = tsk->exec_file;
5482 +
5483 +       if (tsk && filp) {
5484 +               tsk->is_writable = 0;
5485 +
5486 +               acl_sp_role_value = (acl_sp_role_value % 65535) + 1;
5487 +               tsk->acl_sp_role = 1;
5488 +               tsk->acl_role_id = acl_sp_role_value;
5489 +               tsk->role = assigned;
5490 +               tsk->acl =
5491 +                   chk_subj_label(filp->f_dentry, filp->f_vfsmnt, tsk->role);
5492 +
5493 +               /* ignore additional mmap checks for processes that are writable 
5494 +                  by the default ACL */
5495 +               obj = chk_obj_label(filp->f_dentry, filp->f_vfsmnt, default_role->root_label);
5496 +               if (unlikely(obj->mode & GR_WRITE))
5497 +                       tsk->is_writable = 1;
5498 +               obj = chk_obj_label(filp->f_dentry, filp->f_vfsmnt, tsk->role->root_label);
5499 +               if (unlikely(obj->mode & GR_WRITE))
5500 +                       tsk->is_writable = 1;
5501 +
5502 +#ifdef CONFIG_GRKERNSEC_ACL_DEBUG
5503 +               printk(KERN_ALERT "Assigning special role:%s subject:%s to process (%s:%d)\n", tsk->role->rolename, tsk->acl->filename, tsk->comm, tsk->pid);
5504 +#endif
5505 +       }
5506 +
5507 +       return;
5508 +}
5509 +
5510 +ssize_t
5511 +write_grsec_handler(struct file *file, const char * buf, size_t count, loff_t *ppos)
5512 +{
5513 +       struct gr_arg *arg;
5514 +       struct gr_arg usermode;
5515 +       unsigned char *sprole_salt;
5516 +       unsigned char *sprole_sum;
5517 +       int error = sizeof (struct gr_arg);
5518 +       int error2 = 0;
5519 +
5520 +       down(&gr_dev_sem);
5521 +
5522 +       arg = (struct gr_arg *) buf;
5523 +
5524 +       if (count != sizeof (struct gr_arg)) {
5525 +               security_alert_good(GR_DEV_ACL_MSG, count,
5526 +                                   (int) sizeof (struct gr_arg));
5527 +               error = -EINVAL;
5528 +               goto out;
5529 +       }
5530 +
5531 +       if ((gr_auth_attempts >= CONFIG_GRKERNSEC_ACL_MAXTRIES)
5532 +           && time_before_eq(gr_auth_expires, jiffies)) {
5533 +               gr_auth_expires = 0;
5534 +               gr_auth_attempts = 0;
5535 +       }
5536 +
5537 +       if (copy_from_user(&usermode, arg, sizeof (struct gr_arg))) {
5538 +               error = -EFAULT;
5539 +               goto out;
5540 +       }
5541 +
5542 +       if (usermode.mode != SPROLE && time_after(gr_auth_expires, jiffies)) {
5543 +               error = -EBUSY;
5544 +               goto out;
5545 +       }
5546 +
5547 +       /* if non-root trying to do anything other than use a special role,
5548 +          do not attempt authentication, do not count towards authentication
5549 +          locking
5550 +        */
5551 +
5552 +       if (usermode.mode != SPROLE && current->uid) {
5553 +               error = -EPERM;
5554 +               goto out;
5555 +       }
5556 +
5557 +       /* ensure pw and special role name are null terminated */
5558 +
5559 +       usermode.pw[GR_PW_LEN - 1] = '\0';
5560 +       usermode.sp_role[GR_SPROLE_LEN - 1] = '\0';
5561 +
5562 +       /* Okay. 
5563 +        * We have our enough of the argument structure..(we have yet
5564 +        * to copy_from_user the tables themselves) . Copy the tables
5565 +        * only if we need them, i.e. for loading operations. */
5566 +
5567 +       switch (usermode.mode) {
5568 +       case STATUS:
5569 +                       if (gr_status & GR_READY)
5570 +                               error = 1;
5571 +                       else
5572 +                               error = 2;
5573 +                       goto out;
5574 +       case SHUTDOWN:
5575 +               if ((gr_status & GR_READY)
5576 +                   && !(chkpw(&usermode, system_salt, system_sum))) {
5577 +                       gr_status &= ~GR_READY;
5578 +                       security_alert_good(GR_SHUTS_ACL_MSG, DEFAULTSECARGS);
5579 +                       free_variables();
5580 +                       memset(&usermode, 0, sizeof (struct gr_arg));
5581 +                       memset(&system_salt, 0, sizeof (system_salt));
5582 +                       memset(&system_sum, 0, sizeof (system_sum));
5583 +               } else if (gr_status & GR_READY) {
5584 +                       security_alert(GR_SHUTF_ACL_MSG, DEFAULTSECARGS);
5585 +                       error = -EPERM;
5586 +               } else {
5587 +                       security_alert_good(GR_SHUTI_ACL_MSG, DEFAULTSECARGS);
5588 +                       error = -EAGAIN;
5589 +               }
5590 +               break;
5591 +       case ENABLE:
5592 +               if (!(gr_status & GR_READY) && !(error2 = gracl_init(&usermode)))
5593 +                       security_alert_good(GR_ENABLE_ACL_MSG, GR_VERSION);
5594 +               else {
5595 +                       if (gr_status & GR_READY)
5596 +                               error = -EAGAIN;
5597 +                       else
5598 +                               error = error2;
5599 +                       security_alert(GR_ENABLEF_ACL_MSG, GR_VERSION,
5600 +                                      DEFAULTSECARGS);
5601 +               }
5602 +               break;
5603 +       case RELOAD:
5604 +               if (!(gr_status & GR_READY)) {
5605 +                       security_alert_good(GR_RELOADI_ACL_MSG);
5606 +                       error = -EAGAIN;
5607 +               } else if (!(chkpw(&usermode, system_salt, system_sum))) {
5608 +                       lock_kernel();
5609 +                       gr_status &= ~GR_READY;
5610 +                       free_variables();
5611 +                       if (!(error2 = gracl_init(&usermode))) {
5612 +                               unlock_kernel();
5613 +                               security_alert_good(GR_RELOAD_ACL_MSG,
5614 +                                                   GR_VERSION);
5615 +                       } else {
5616 +                               unlock_kernel();
5617 +                               error = error2;
5618 +                               security_alert(GR_RELOADF_ACL_MSG, GR_VERSION,
5619 +                                              DEFAULTSECARGS);
5620 +                       }
5621 +               } else {
5622 +                       security_alert(GR_RELOADF_ACL_MSG, GR_VERSION,
5623 +                                      DEFAULTSECARGS);
5624 +                       error = -EPERM;
5625 +               }
5626 +               break;
5627 +       case SEGVMOD:
5628 +               if (unlikely(!(gr_status & GR_READY))) {
5629 +                       security_alert_good(GR_SEGVMODI_ACL_MSG,
5630 +                                           DEFAULTSECARGS);
5631 +                       error = -EAGAIN;
5632 +                       break;
5633 +               }
5634 +
5635 +               if (!(chkpw(&usermode, system_salt, system_sum))) {
5636 +                       security_alert_good(GR_SEGVMODS_ACL_MSG,
5637 +                                           DEFAULTSECARGS);
5638 +                       if (usermode.segv_device && usermode.segv_inode) {
5639 +                               struct acl_subject_label *segvacl;
5640 +                               segvacl =
5641 +                                   lookup_acl_subj_label(usermode.segv_inode,
5642 +                                                         usermode.segv_device,
5643 +                                                         current->role);
5644 +                               if (segvacl) {
5645 +                                       segvacl->crashes = 0;
5646 +                                       segvacl->expires = 0;
5647 +                               }
5648 +                       } else if (gr_find_uid(usermode.segv_uid) >= 0) {
5649 +                               gr_remove_uid(usermode.segv_uid);
5650 +                       }
5651 +               } else {
5652 +                       security_alert(GR_SEGVMODF_ACL_MSG, DEFAULTSECARGS);
5653 +                       error = -EPERM;
5654 +               }
5655 +               break;
5656 +       case SPROLE:
5657 +               if (unlikely(!(gr_status & GR_READY))) {
5658 +                       security_alert_good(GR_SPROLEI_ACL_MSG, DEFAULTSECARGS);
5659 +                       error = -EAGAIN;
5660 +                       break;
5661 +               }
5662 +
5663 +               if ((current->role->auth_attempts >= CONFIG_GRKERNSEC_ACL_MAXTRIES)
5664 +                   && time_before_eq(current->role->expires, jiffies)) {
5665 +                       current->role->expires = 0;
5666 +                       current->role->auth_attempts = 0;
5667 +               }
5668 +
5669 +               if (time_after(current->role->expires, jiffies)) {
5670 +                       error = -EBUSY;
5671 +                       goto out;
5672 +               }
5673 +
5674 +               if (lookup_special_role_auth
5675 +                   (usermode.sp_role, &sprole_salt, &sprole_sum)
5676 +                   && ((!sprole_salt && !sprole_sum)
5677 +                       || !(chkpw(&usermode, sprole_salt, sprole_sum)))) {
5678 +                       assign_special_role(usermode.sp_role);
5679 +                       security_alert_good(GR_SPROLES_ACL_MSG,
5680 +                                           (current->p_pptr) ? current->
5681 +                                           p_pptr->role->rolename : "",
5682 +                                           acl_sp_role_value, DEFAULTSECARGS);
5683 +               } else {
5684 +                       security_alert(GR_SPROLEF_ACL_MSG, usermode.sp_role,
5685 +                                      DEFAULTSECARGS);
5686 +                       error = -EPERM;
5687 +                       current->role->auth_attempts++;
5688 +                       if (current->role->auth_attempts >= CONFIG_GRKERNSEC_ACL_MAXTRIES) {
5689 +                               current->role->expires =
5690 +                                   jiffies + CONFIG_GRKERNSEC_ACL_TIMEOUT * HZ;
5691 +                               security_alert(GR_MAXROLEPW_ACL_MSG,
5692 +                                      CONFIG_GRKERNSEC_ACL_MAXTRIES,
5693 +                                      usermode.sp_role, DEFAULTSECARGS);
5694 +                       }
5695 +
5696 +                       goto out;
5697 +               }
5698 +               break;
5699 +       default:
5700 +               security_alert(GR_INVMODE_ACL_MSG, usermode.mode,
5701 +                              DEFAULTSECARGS);
5702 +               error = -EINVAL;
5703 +               break;
5704 +       }
5705 +
5706 +       if (error != -EPERM)
5707 +               goto out;
5708 +
5709 +       gr_auth_attempts++;
5710 +
5711 +       if (gr_auth_attempts >= CONFIG_GRKERNSEC_ACL_MAXTRIES) {
5712 +               security_alert(GR_MAXPW_ACL_MSG, CONFIG_GRKERNSEC_ACL_MAXTRIES);
5713 +               gr_auth_expires = jiffies + CONFIG_GRKERNSEC_ACL_TIMEOUT * HZ;
5714 +       }
5715 +
5716 +      out:
5717 +       up(&gr_dev_sem);
5718 +       return error;
5719 +}
5720 +
5721 +int
5722 +gr_set_acls(const int type)
5723 +{
5724 +       struct acl_object_label *obj;
5725 +       struct task_struct *task;
5726 +       struct file *filp;
5727 +       unsigned short i;
5728 +
5729 +       read_lock(&tasklist_lock);
5730 +       for_each_task(task) {
5731 +               /* check to see if we're called from the exit handler,
5732 +                  if so, only replace ACLs that have inherited the admin
5733 +                  ACL */
5734 +
5735 +               if (type && (task->role != current->role ||
5736 +                            task->acl_role_id != current->acl_role_id))
5737 +                       continue;
5738 +
5739 +               task->acl_role_id = 0;
5740 +
5741 +               if ((filp = task->exec_file)) {
5742 +                       do_set_role_label(task, task->uid, task->gid);
5743 +
5744 +                       task->acl =
5745 +                           chk_subj_label(filp->f_dentry, filp->f_vfsmnt,
5746 +                                          task->role);
5747 +                       if (task->acl) {
5748 +                               struct acl_subject_label *curr;
5749 +                               curr = task->acl;
5750 +
5751 +                               task->is_writable = 0;
5752 +                               /* ignore additional mmap checks for processes that are writable 
5753 +                                  by the default ACL */
5754 +                               obj = chk_obj_label(filp->f_dentry, filp->f_vfsmnt, default_role->root_label);
5755 +                               if (unlikely(obj->mode & GR_WRITE))
5756 +                                       task->is_writable = 1;
5757 +                               obj = chk_obj_label(filp->f_dentry, filp->f_vfsmnt, task->role->root_label);
5758 +                               if (unlikely(obj->mode & GR_WRITE))
5759 +                                       task->is_writable = 1;
5760 +
5761 +#ifdef CONFIG_GRKERNSEC_ACL_DEBUG
5762 +                               printk(KERN_ALERT "gr_set_acls for (%s:%d): role:%s, subject:%s\n", task->comm, task->pid, task->role->rolename, task->acl->filename);
5763 +#endif
5764 +                               if (!(curr->mode & GR_LEARN))
5765 +                                       for (i = 0; i < RLIM_NLIMITS; i++) {
5766 +                                               if (!(curr->resmask & (1 << i)))
5767 +                                                       continue;
5768 +
5769 +                                               task->rlim[i].rlim_cur =
5770 +                                                   curr->res[i].rlim_cur;
5771 +                                               task->rlim[i].rlim_max =
5772 +                                                   curr->res[i].rlim_max;
5773 +                                       }
5774 +                       } else {
5775 +                               read_unlock(&tasklist_lock);
5776 +                               security_alert_good(GR_DEFACL_MSG, task->comm,
5777 +                                                   task->pid);
5778 +                               return 1;
5779 +                       }
5780 +               } else {
5781 +                       // it's a kernel process
5782 +                       task->role = kernel_role;
5783 +                       task->acl = kernel_role->root_label;
5784 +#ifdef CONFIG_GRKERNSEC_ACL_HIDEKERN
5785 +                       task->acl->mode &= ~GR_FIND;
5786 +#endif
5787 +               }
5788 +       }
5789 +       read_unlock(&tasklist_lock);
5790 +       return 0;
5791 +}
5792 +
5793 +void
5794 +gr_learn_resource(const struct task_struct *task,
5795 +                 const int res, const unsigned long wanted)
5796 +{
5797 +       struct acl_subject_label *acl;
5798 +
5799 +       if (unlikely((gr_status & GR_READY) &&
5800 +                    task->acl && (task->acl->mode & GR_LEARN)))
5801 +               goto skip_reslog;
5802 +
5803 +#ifdef CONFIG_GRKERNSEC_RESLOG
5804 +       gr_log_resource(task, res, wanted);
5805 +#endif
5806 +      skip_reslog:
5807 +
5808 +       if (unlikely(!(gr_status & GR_READY) || !wanted))
5809 +               return;
5810 +
5811 +       acl = task->acl;
5812 +
5813 +       if (likely(!acl || !(acl->mode & GR_LEARN) ||
5814 +                  !(acl->resmask & (1 << (unsigned short) res))))
5815 +               return;
5816 +
5817 +       if (wanted >= acl->res[res].rlim_cur) {
5818 +               unsigned long res_add;
5819 +
5820 +               res_add = wanted;
5821 +               switch (res) {
5822 +               case RLIMIT_CPU:
5823 +                       res_add += GR_RLIM_CPU_BUMP;
5824 +                       break;
5825 +               case RLIMIT_FSIZE:
5826 +                       res_add += GR_RLIM_FSIZE_BUMP;
5827 +                       break;
5828 +               case RLIMIT_DATA:
5829 +                       res_add += GR_RLIM_DATA_BUMP;
5830 +                       break;
5831 +               case RLIMIT_STACK:
5832 +                       res_add += GR_RLIM_STACK_BUMP;
5833 +                       break;
5834 +               case RLIMIT_CORE:
5835 +                       res_add += GR_RLIM_CORE_BUMP;
5836 +                       break;
5837 +               case RLIMIT_RSS:
5838 +                       res_add += GR_RLIM_RSS_BUMP;
5839 +                       break;
5840 +               case RLIMIT_NPROC:
5841 +                       res_add += GR_RLIM_NPROC_BUMP;
5842 +                       break;
5843 +               case RLIMIT_NOFILE:
5844 +                       res_add += GR_RLIM_NOFILE_BUMP;
5845 +                       break;
5846 +               case RLIMIT_MEMLOCK:
5847 +                       res_add += GR_RLIM_MEMLOCK_BUMP;
5848 +                       break;
5849 +               case RLIMIT_AS:
5850 +                       res_add += GR_RLIM_AS_BUMP;
5851 +                       break;
5852 +               case RLIMIT_LOCKS:
5853 +                       res_add += GR_RLIM_LOCKS_BUMP;
5854 +                       break;
5855 +               }
5856 +
5857 +               acl->res[res].rlim_cur = res_add;
5858 +
5859 +               if (wanted > acl->res[res].rlim_max)
5860 +                       acl->res[res].rlim_max = res_add;
5861 +
5862 +               security_learn(GR_LEARN_AUDIT_MSG, current->role->rolename,
5863 +                              current->role->roletype, acl->filename,
5864 +                              acl->res[res].rlim_cur, acl->res[res].rlim_max,
5865 +                              "", (unsigned long) res);
5866 +       }
5867 +
5868 +       return;
5869 +}
5870 +
5871 +#ifdef CONFIG_SYSCTL
5872 +extern struct proc_dir_entry *proc_sys_root;
5873 +
5874 +__u32
5875 +gr_handle_sysctl(const struct ctl_table *table, const void *oldval,
5876 +                const void *newval)
5877 +{
5878 +       struct proc_dir_entry *tmp;
5879 +       struct nameidata nd;
5880 +       const char *proc_sys = "/proc/sys";
5881 +       char *path = gr_shared_page[0][smp_processor_id()];
5882 +       struct acl_object_label *obj;
5883 +       unsigned short len = 0, pos = 0, depth = 0, i;
5884 +       __u32 err = 0;
5885 +       __u32 mode = 0;
5886 +
5887 +       if (unlikely(!(gr_status & GR_READY)))
5888 +               return 1;
5889 +
5890 +       if (oldval)
5891 +               mode |= GR_READ;
5892 +       if (newval)
5893 +               mode |= GR_WRITE;
5894 +
5895 +       /* convert the requested sysctl entry into a pathname */
5896 +
5897 +       for (tmp = table->de; tmp != proc_sys_root; tmp = tmp->parent) {
5898 +               len += strlen(tmp->name);
5899 +               len++;
5900 +               depth++;
5901 +       }
5902 +
5903 +       if ((len + depth + strlen(proc_sys) + 1) > PAGE_SIZE)
5904 +               return 0;       // deny
5905 +
5906 +       memset(path, 0, PAGE_SIZE);
5907 +
5908 +       memcpy(path, proc_sys, strlen(proc_sys));
5909 +
5910 +       pos += strlen(proc_sys);
5911 +
5912 +       for (; depth > 0; depth--) {
5913 +               path[pos] = '/';
5914 +               pos++;
5915 +               for (i = 1, tmp = table->de; tmp != proc_sys_root;
5916 +                    tmp = tmp->parent) {
5917 +                       if (depth == i) {
5918 +                               memcpy(path + pos, tmp->name,
5919 +                                      strlen(tmp->name));
5920 +                               pos += strlen(tmp->name);
5921 +                       }
5922 +                       i++;
5923 +               }
5924 +       }
5925 +
5926 +       if (path_init(path, LOOKUP_FOLLOW, &nd))
5927 +               err = path_walk(path, &nd);
5928 +
5929 +       if (err)
5930 +               goto out;
5931 +
5932 +       obj = chk_obj_label(nd.dentry, nd.mnt, current->acl);
5933 +       err = obj->mode & (mode | to_gr_audit(mode) | GR_SUPPRESS);
5934 +
5935 +       if (unlikely((current->acl->mode & GR_LEARN) && ((err & mode) != mode))) {
5936 +               __u32 new_mode = mode;
5937 +
5938 +               new_mode &= ~(GR_AUDITS | GR_SUPPRESS);
5939 +
5940 +               err = new_mode;
5941 +               gr_log_learn(current->role, current->uid, current->gid,
5942 +                            current, path, new_mode);
5943 +       } else if ((err & mode) != mode && !(err & GR_SUPPRESS)) {
5944 +               security_alert(GR_SYSCTL_ACL_MSG, "denied", path,
5945 +                              (mode & GR_READ) ? " reading" : "",
5946 +                              (mode & GR_WRITE) ? " writing" : "",
5947 +                              DEFAULTSECARGS);
5948 +               err = 0;
5949 +       } else if ((err & mode) != mode) {
5950 +               err = 0;
5951 +       } else if (((err & mode) == mode) && (err & GR_AUDITS)) {
5952 +               security_audit(GR_SYSCTL_ACL_MSG, "successful",
5953 +                              path, (mode & GR_READ) ? " reading" : "",
5954 +                              (mode & GR_WRITE) ? " writing" : "",
5955 +                              DEFAULTSECARGS);
5956 +       }
5957 +
5958 +       path_release(&nd);
5959 +
5960 +      out:
5961 +       return err;
5962 +}
5963 +#endif
5964 +
5965 +int
5966 +gr_handle_ptrace(struct task_struct *task, const long request)
5967 +{
5968 +       struct file *filp;
5969 +       __u32 retmode;
5970 +
5971 +       if (unlikely(!(gr_status & GR_READY)))
5972 +               return 0;
5973 +
5974 +       filp = task->exec_file;
5975 +
5976 +       if (unlikely(!filp))
5977 +               return 0;
5978 +
5979 +       retmode = gr_search_file(filp->f_dentry, GR_PTRACERD, filp->f_vfsmnt);
5980 +
5981 +       if (retmode & GR_PTRACERD) {
5982 +               switch (request) {
5983 +               case PTRACE_POKETEXT:
5984 +               case PTRACE_POKEDATA:
5985 +               case PTRACE_POKEUSR:
5986 +#if !defined(CONFIG_PPC32) && !defined(CONFIG_PARISC) && !defined(CONFIG_ALPHA)
5987 +               case PTRACE_SETREGS:
5988 +               case PTRACE_SETFPREGS:
5989 +#endif
5990 +#ifdef CONFIG_X86
5991 +               case PTRACE_SETFPXREGS:
5992 +#endif
5993 +#ifdef CONFIG_ALTIVEC
5994 +               case PTRACE_SETVRREGS:
5995 +#endif
5996 +                       return 1;
5997 +               default:
5998 +                       return 0;
5999 +               }
6000 +       } else if (!(current->acl->mode & GR_OVERRIDE) &&
6001 +                  !(current->role->roletype & GR_ROLE_GOD)
6002 +                  && (current->acl != task->acl
6003 +                      || (current->acl != current->role->root_label
6004 +                          && current->pid != task->pid))) {
6005 +               security_alert(GR_PTRACE_ACL_MSG,
6006 +                              gr_to_filename(filp->f_dentry, filp->f_vfsmnt),
6007 +                              task->comm, task->pid, DEFAULTSECARGS);
6008 +               return 1;
6009 +       }
6010 +
6011 +       return 0;
6012 +}
6013 +
6014 +int
6015 +gr_handle_ptrace_exec(const struct dentry *dentry, const struct vfsmount *mnt)
6016 +{
6017 +       __u32 retmode;
6018 +       struct acl_subject_label *subj;
6019 +
6020 +       if (unlikely(!(gr_status & GR_READY)))
6021 +               return 0;
6022 +
6023 +       if (unlikely
6024 +           ((current->ptrace & PT_PTRACED)
6025 +            && !(current->acl->mode & GR_OVERRIDE)))
6026 +               retmode = gr_search_file(dentry, GR_PTRACERD, mnt);
6027 +       else
6028 +               return 0;
6029 +
6030 +       subj = chk_subj_label(dentry, mnt, current->role);
6031 +
6032 +       if (!(retmode & GR_PTRACERD) &&
6033 +           !(current->role->roletype & GR_ROLE_GOD) &&
6034 +           (current->acl != subj)) {
6035 +               security_alert(GR_PTRACE_EXEC_ACL_MSG,
6036 +                              gr_to_filename(dentry, mnt), DEFAULTSECARGS);
6037 +               return 1;
6038 +       }
6039 +
6040 +       return 0;
6041 +}
6042 +
6043 +int
6044 +gr_handle_mmap(const struct file *filp, const unsigned long prot)
6045 +{
6046 +       struct acl_object_label *obj, *obj2;
6047 +
6048 +       if (unlikely(!(gr_status & GR_READY) ||
6049 +                    (current->acl->mode & GR_OVERRIDE) || !filp ||
6050 +                    !(prot & PROT_EXEC)))
6051 +               return 0;
6052 +
6053 +       if (unlikely(current->is_writable))
6054 +               return 0;
6055 +
6056 +       obj = chk_obj_label(filp->f_dentry, filp->f_vfsmnt, default_role->root_label);
6057 +       obj2 = chk_obj_label(filp->f_dentry, filp->f_vfsmnt,
6058 +                            current->role->root_label);
6059 +       if (unlikely((obj->mode & GR_WRITE) || (obj2->mode & GR_WRITE))) {
6060 +               security_alert(GR_WRITLIB_ACL_MSG,
6061 +                              gr_to_filename(filp->f_dentry, filp->f_vfsmnt),
6062 +                              DEFAULTSECARGS);
6063 +               return 1;
6064 +       }
6065 +
6066 +       return 0;
6067 +}
6068 +
6069 +int
6070 +gr_acl_handle_mmap(const struct file *file, const unsigned long prot)
6071 +{
6072 +       __u32 mode;
6073 +
6074 +       if (unlikely(!file || !(prot & PROT_EXEC)))
6075 +               return 1;
6076 +
6077 +       mode =
6078 +           gr_search_file(file->f_dentry,
6079 +                          GR_EXEC | GR_AUDIT_EXEC | GR_SUPPRESS,
6080 +                          file->f_vfsmnt);
6081 +
6082 +       if (unlikely(!gr_tpe_allow(file) || (!(mode & GR_EXEC) && !(mode & GR_SUPPRESS)))) {
6083 +               security_alert(GR_MMAP_ACL_MSG, "denied",
6084 +                              gr_to_filename(file->f_dentry, file->f_vfsmnt),
6085 +                              DEFAULTSECARGS);
6086 +               return 0;
6087 +       } else if (unlikely(!gr_tpe_allow(file) || !(mode & GR_EXEC))) {
6088 +               return 0;
6089 +       } else if (unlikely(mode & GR_EXEC && mode & GR_AUDIT_EXEC)) {
6090 +               security_audit(GR_MMAP_ACL_MSG, "successful",
6091 +                              gr_to_filename(file->f_dentry, file->f_vfsmnt),
6092 +                              DEFAULTSECARGS);
6093 +               return 1;
6094 +       }
6095 +
6096 +       return 1;
6097 +}
6098 +
6099 +int
6100 +gr_acl_handle_mprotect(const struct file *file, const unsigned long prot)
6101 +{
6102 +       __u32 mode;
6103 +
6104 +       if (unlikely(!file || !(prot & PROT_EXEC)))
6105 +               return 1;
6106 +
6107 +       mode =
6108 +           gr_search_file(file->f_dentry,
6109 +                          GR_EXEC | GR_AUDIT_EXEC | GR_SUPPRESS,
6110 +                          file->f_vfsmnt);
6111 +
6112 +       if (unlikely(!gr_tpe_allow(file) || (!(mode & GR_EXEC) && !(mode & GR_SUPPRESS)))) {
6113 +               security_alert(GR_MPROTECT_ACL_MSG, "denied",
6114 +                              gr_to_filename(file->f_dentry, file->f_vfsmnt),
6115 +                              DEFAULTSECARGS);
6116 +               return 0;
6117 +       } else if (unlikely(!gr_tpe_allow(file) || !(mode & GR_EXEC))) {
6118 +               return 0;
6119 +       } else if (unlikely(mode & GR_EXEC && mode & GR_AUDIT_EXEC)) {
6120 +               security_audit(GR_MPROTECT_ACL_MSG, "successful",
6121 +                              gr_to_filename(file->f_dentry, file->f_vfsmnt),
6122 +                              DEFAULTSECARGS);
6123 +               return 1;
6124 +       }
6125 +
6126 +       return 1;
6127 +}
6128 +
6129 +void
6130 +gr_acl_handle_psacct(struct task_struct *task, const long code)
6131 +{
6132 +       unsigned long runtime;
6133 +       unsigned long cputime;
6134 +       unsigned int wday, cday;
6135 +       __u8 whr, chr;
6136 +       __u8 wmin, cmin;
6137 +       __u8 wsec, csec;
6138 +       char cur_tty[64] = { 0 };
6139 +       char parent_tty[64] = { 0 };
6140 +
6141 +       if (unlikely(!(gr_status & GR_READY) || !task->acl ||
6142 +                    !(task->acl->mode & GR_PROCACCT)))
6143 +               return;
6144 +
6145 +       runtime = (jiffies - task->start_time) / HZ;
6146 +       wday = runtime / (3600 * 24);
6147 +       runtime -= wday * (3600 * 24);
6148 +       whr = runtime / 3600;
6149 +       runtime -= whr * 3600;
6150 +       wmin = runtime / 60;
6151 +       runtime -= wmin * 60;
6152 +       wsec = runtime;
6153 +
6154 +       cputime = (task->times.tms_utime + task->times.tms_stime) / HZ;
6155 +       cday = cputime / (3600 * 24);
6156 +       cputime -= cday * (3600 * 24);
6157 +       chr = cputime / 3600;
6158 +       cputime -= chr * 3600;
6159 +       cmin = cputime / 60;
6160 +       cputime -= cmin * 60;
6161 +       csec = cputime;
6162 +
6163 +       security_audit(GR_ACL_PROCACCT_MSG, task->comm,
6164 +                      task->pid, NIPQUAD(task->curr_ip), tty_name(task->tty,
6165 +                                                                  cur_tty),
6166 +                      task->uid, task->euid, task->gid, task->egid, wday, whr,
6167 +                      wmin, wsec, cday, chr, cmin, csec,
6168 +                      (task->
6169 +                       flags & PF_SIGNALED) ? "killed by signal" : "exited",
6170 +                      code, task->p_pptr->comm, task->p_pptr->pid,
6171 +                      NIPQUAD(task->p_pptr->curr_ip),
6172 +                      tty_name(task->p_pptr->tty, parent_tty),
6173 +                      task->p_pptr->uid, task->p_pptr->euid, task->p_pptr->gid,
6174 +                      task->p_pptr->egid);
6175 +
6176 +       return;
6177 +}
6178 +
6179 +void gr_set_kernel_label(struct task_struct *task)
6180 +{
6181 +       if (gr_status & GR_READY) {
6182 +               task->role = kernel_role;
6183 +               task->acl = kernel_role->root_label;
6184 +       }
6185 +       return;
6186 +}
6187 diff -urN linux-2.4.21/grsecurity/gracl_alloc.c linux-2.4.21/grsecurity/gracl_alloc.c
6188 --- linux-2.4.21/grsecurity/gracl_alloc.c       1969-12-31 19:00:00.000000000 -0500
6189 +++ linux-2.4.21/grsecurity/gracl_alloc.c       2003-06-23 11:49:16.000000000 -0400
6190 @@ -0,0 +1,94 @@
6191 +/* stack-based acl allocation tracking (c) Brad Spengler 2002,2003 */
6192 +
6193 +#include <linux/kernel.h>
6194 +#include <linux/mm.h>
6195 +#include <linux/slab.h>
6196 +#include <linux/vmalloc.h>
6197 +#include <linux/gracl.h>
6198 +#include <linux/grsecurity.h>
6199 +
6200 +static unsigned long alloc_stack_next = 1;
6201 +static unsigned long alloc_stack_size = 1;
6202 +static void **alloc_stack;
6203 +
6204 +static __inline__ int
6205 +alloc_pop(void)
6206 +{
6207 +       if ((alloc_stack_next - 1) == 0)
6208 +               return 0;
6209 +
6210 +       if (*(alloc_stack + alloc_stack_next - 2))
6211 +               kfree(*(alloc_stack + alloc_stack_next - 2));
6212 +
6213 +       alloc_stack_next--;
6214 +
6215 +       return 1;
6216 +}
6217 +
6218 +static __inline__ void
6219 +alloc_push(void *buf)
6220 +{
6221 +       if (alloc_stack_next >= alloc_stack_size)
6222 +               BUG();
6223 +
6224 +       *(alloc_stack + alloc_stack_next - 1) = buf;
6225 +
6226 +       alloc_stack_next++;
6227 +
6228 +       return;
6229 +}
6230 +
6231 +void *
6232 +acl_alloc(unsigned long len)
6233 +{
6234 +       void *ret;
6235 +
6236 +       if (len > PAGE_SIZE)
6237 +               BUG();
6238 +
6239 +       ret = kmalloc(len, GFP_KERNEL);
6240 +
6241 +       if (ret)
6242 +               alloc_push(ret);
6243 +
6244 +       return ret;
6245 +}
6246 +
6247 +void
6248 +acl_free_all(void)
6249 +{
6250 +       if (gr_acl_is_enabled() || !alloc_stack)
6251 +               return;
6252 +
6253 +       while (alloc_pop()) ;
6254 +
6255 +       if (alloc_stack) {
6256 +               if ((alloc_stack_size * sizeof (void *)) <= PAGE_SIZE)
6257 +                       kfree(alloc_stack);
6258 +               else
6259 +                       vfree(alloc_stack);
6260 +       }
6261 +
6262 +       alloc_stack = NULL;
6263 +       alloc_stack_size = 1;
6264 +       alloc_stack_next = 1;
6265 +
6266 +       return;
6267 +}
6268 +
6269 +int
6270 +acl_alloc_stack_init(unsigned long size)
6271 +{
6272 +       if ((size * sizeof (void *)) <= PAGE_SIZE)
6273 +               alloc_stack =
6274 +                   (void **) kmalloc(size * sizeof (void *), GFP_KERNEL);
6275 +       else
6276 +               alloc_stack = (void **) vmalloc(size * sizeof (void *));
6277 +
6278 +       alloc_stack_size = size;
6279 +
6280 +       if (!alloc_stack)
6281 +               return 0;
6282 +       else
6283 +               return 1;
6284 +}
6285 diff -urN linux-2.4.21/grsecurity/gracl_cap.c linux-2.4.21/grsecurity/gracl_cap.c
6286 --- linux-2.4.21/grsecurity/gracl_cap.c 1969-12-31 19:00:00.000000000 -0500
6287 +++ linux-2.4.21/grsecurity/gracl_cap.c 2003-06-23 11:49:16.000000000 -0400
6288 @@ -0,0 +1,71 @@
6289 +/* capability handling routines, (c) Brad Spengler 2002,2003 */
6290 +
6291 +#include <linux/kernel.h>
6292 +#include <linux/sched.h>
6293 +#include <linux/capability.h>
6294 +#include <linux/gracl.h>
6295 +#include <linux/grsecurity.h>
6296 +#include <linux/grinternal.h>
6297 +
6298 +static const char *captab_log[29] = {
6299 +       "CAP_CHOWN",
6300 +       "CAP_DAC_OVERRIDE",
6301 +       "CAP_DAC_READ_SEARCH",
6302 +       "CAP_FOWNER",
6303 +       "CAP_FSETID",
6304 +       "CAP_KILL",
6305 +       "CAP_SETGID",
6306 +       "CAP_SETUID",
6307 +       "CAP_SETPCAP",
6308 +       "CAP_LINUX_IMMUTABLE",
6309 +       "CAP_NET_BIND_SERVICE",
6310 +       "CAP_NET_BROADCAST",
6311 +       "CAP_NET_ADMIN",
6312 +       "CAP_NET_RAW",
6313 +       "CAP_IPC_LOCK",
6314 +       "CAP_IPC_OWNER",
6315 +       "CAP_SYS_MODULE",
6316 +       "CAP_SYS_RAWIO",
6317 +       "CAP_SYS_CHROOT",
6318 +       "CAP_SYS_PTRACE",
6319 +       "CAP_SYS_PACCT",
6320 +       "CAP_SYS_ADMIN",
6321 +       "CAP_SYS_BOOT",
6322 +       "CAP_SYS_NICE",
6323 +       "CAP_SYS_RESOURCE",
6324 +       "CAP_SYS_TIME",
6325 +       "CAP_SYS_TTY_CONFIG",
6326 +       "CAP_MKNOD",
6327 +       "CAP_LEASE"
6328 +};
6329 +
6330 +int
6331 +gr_is_capable(const int cap)
6332 +{
6333 +       struct acl_subject_label *curracl;
6334 +
6335 +       if (!gr_acl_is_enabled())
6336 +               return 1;
6337 +
6338 +       curracl = current->acl;
6339 +
6340 +       if (!cap_raised(curracl->cap_lower, cap))
6341 +               return 1;
6342 +
6343 +       if ((curracl->mode & GR_LEARN)
6344 +           && cap_raised(current->cap_effective, cap)) {
6345 +               security_learn(GR_LEARN_AUDIT_MSG, current->role->rolename,
6346 +                              current->role->roletype, current->uid,
6347 +                              current->gid, current->exec_file ?
6348 +                              gr_to_filename(current->exec_file->f_dentry,
6349 +                              current->exec_file->f_vfsmnt) : curracl->filename,
6350 +                              curracl->filename, 0UL,
6351 +                              0UL, "", (unsigned long) cap, NIPQUAD(current->curr_ip));
6352 +               return 1;
6353 +       }
6354 +
6355 +       if ((cap >= 0) && (cap < 29) && cap_raised(current->cap_effective, cap))
6356 +               security_alert(GR_CAP_ACL_MSG, captab_log[cap], DEFAULTSECARGS);
6357 +
6358 +       return 0;
6359 +}
6360 diff -urN linux-2.4.21/grsecurity/gracl_fs.c linux-2.4.21/grsecurity/gracl_fs.c
6361 --- linux-2.4.21/grsecurity/gracl_fs.c  1969-12-31 19:00:00.000000000 -0500
6362 +++ linux-2.4.21/grsecurity/gracl_fs.c  2003-06-23 11:49:16.000000000 -0400
6363 @@ -0,0 +1,469 @@
6364 +#include <linux/kernel.h>
6365 +#include <linux/sched.h>
6366 +#include <linux/types.h>
6367 +#include <linux/fs.h>
6368 +#include <linux/file.h>
6369 +#include <linux/grsecurity.h>
6370 +#include <linux/grinternal.h>
6371 +#include <linux/gracl.h>
6372 +
6373 +__u32
6374 +gr_acl_handle_hidden_file(const struct dentry * dentry,
6375 +                         const struct vfsmount * mnt)
6376 +{
6377 +       __u32 mode;
6378 +
6379 +       if (unlikely(!dentry->d_inode))
6380 +               return GR_FIND;
6381 +
6382 +       mode =
6383 +           gr_search_file(dentry, GR_FIND | GR_AUDIT_FIND | GR_SUPPRESS, mnt);
6384 +
6385 +       if (unlikely(mode & GR_FIND && mode & GR_AUDIT_FIND)) {
6386 +               security_audit(GR_HIDDEN_ACL_MSG, "successful",
6387 +                              gr_to_filename(dentry, mnt), DEFAULTSECARGS);
6388 +               return mode;
6389 +       } else if (unlikely(!(mode & GR_FIND) && !(mode & GR_SUPPRESS))) {
6390 +               security_alert(GR_HIDDEN_ACL_MSG, "denied",
6391 +                              gr_to_filename(dentry, mnt),
6392 +                              DEFAULTSECARGS);
6393 +               return 0;
6394 +       } else if (unlikely(!(mode & GR_FIND)))
6395 +               return 0;
6396 +
6397 +       return GR_FIND;
6398 +}
6399 +
6400 +__u32
6401 +gr_acl_handle_open(const struct dentry * dentry, const struct vfsmount * mnt,
6402 +                  const int fmode)
6403 +{
6404 +       __u32 reqmode = GR_FIND;
6405 +       __u32 mode;
6406 +
6407 +       if (unlikely(!dentry->d_inode))
6408 +               return reqmode;
6409 +
6410 +       if (unlikely(fmode & O_APPEND))
6411 +               reqmode |= GR_APPEND;
6412 +       else if (unlikely(fmode & FMODE_WRITE))
6413 +               reqmode |= GR_WRITE;
6414 +       if (likely((fmode & FMODE_READ) && !(fmode & O_DIRECTORY)))
6415 +               reqmode |= GR_READ;
6416 +
6417 +       mode =
6418 +           gr_search_file(dentry, reqmode | to_gr_audit(reqmode) | GR_SUPPRESS,
6419 +                          mnt);
6420 +
6421 +       if (unlikely(((mode & reqmode) == reqmode) && mode & GR_AUDITS)) {
6422 +               security_audit(GR_OPEN_ACL_MSG, "successful",
6423 +                              gr_to_filename(dentry, mnt),
6424 +                              reqmode & GR_READ ? " reading" : "",
6425 +                              reqmode & GR_WRITE ? " writing" :
6426 +                              reqmode & GR_APPEND ? " appending" : "",
6427 +                              DEFAULTSECARGS);
6428 +               return reqmode;
6429 +       } else
6430 +           if (unlikely((mode & reqmode) != reqmode && !(mode & GR_SUPPRESS)))
6431 +       {
6432 +               security_alert(GR_OPEN_ACL_MSG, "denied",
6433 +                              gr_to_filename(dentry, mnt),
6434 +                              reqmode & GR_READ ? " reading" : "",
6435 +                              reqmode & GR_WRITE ? " writing" : reqmode &
6436 +                              GR_APPEND ? " appending" : "", DEFAULTSECARGS);
6437 +               return 0;
6438 +       } else if (unlikely((mode & reqmode) != reqmode))
6439 +               return 0;
6440 +
6441 +       return reqmode;
6442 +}
6443 +
6444 +__u32
6445 +gr_acl_handle_creat(const struct dentry * dentry,
6446 +                   const struct dentry * p_dentry,
6447 +                   const struct vfsmount * p_mnt, const int fmode,
6448 +                   const int imode)
6449 +{
6450 +       __u32 reqmode = GR_WRITE | GR_CREATE;
6451 +       __u32 mode;
6452 +
6453 +       if (unlikely(fmode & O_APPEND))
6454 +               reqmode |= GR_APPEND;
6455 +       if (unlikely((fmode & FMODE_READ) && !(fmode & O_DIRECTORY)))
6456 +               reqmode |= GR_READ;
6457 +       if (unlikely((fmode & O_CREAT) && (imode & (S_ISUID | S_ISGID))))
6458 +               reqmode |= GR_SETID;
6459 +
6460 +       mode =
6461 +           gr_check_create(dentry, p_dentry, p_mnt,
6462 +                           reqmode | to_gr_audit(reqmode) | GR_SUPPRESS);
6463 +
6464 +       if (unlikely(((mode & reqmode) == reqmode) && mode & GR_AUDITS)) {
6465 +               security_audit(GR_CREATE_ACL_MSG, "successful",
6466 +                              gr_to_filename(dentry, p_mnt),
6467 +                              reqmode & GR_READ ? " reading" : "",
6468 +                              reqmode & GR_WRITE ? " writing" :
6469 +                              reqmode & GR_APPEND ? " appending" : "",
6470 +                              DEFAULTSECARGS);
6471 +               return reqmode;
6472 +       } else
6473 +           if (unlikely((mode & reqmode) != reqmode && !(mode & GR_SUPPRESS)))
6474 +       {
6475 +               security_alert(GR_CREATE_ACL_MSG, "denied",
6476 +                              gr_to_filename(dentry, p_mnt),
6477 +                              reqmode & GR_READ ? " reading" : "",
6478 +                              reqmode & GR_WRITE ? " writing" : reqmode &
6479 +                              GR_APPEND ? " appending" : "", DEFAULTSECARGS);
6480 +               return 0;
6481 +       } else if (unlikely((mode & reqmode) != reqmode))
6482 +               return 0;
6483 +
6484 +       return reqmode;
6485 +}
6486 +
6487 +__u32
6488 +gr_acl_handle_access(const struct dentry * dentry, const struct vfsmount * mnt,
6489 +                    const int fmode)
6490 +{
6491 +       __u32 mode, reqmode = GR_FIND;
6492 +
6493 +       if ((fmode & S_IXOTH) && !S_ISDIR(dentry->d_inode->i_mode))
6494 +               reqmode |= GR_EXEC;
6495 +       if (fmode & S_IWOTH)
6496 +               reqmode |= GR_WRITE;
6497 +       if (fmode & S_IROTH)
6498 +               reqmode |= GR_READ;
6499 +
6500 +       mode =
6501 +           gr_search_file(dentry, reqmode | to_gr_audit(reqmode) | GR_SUPPRESS,
6502 +                          mnt);
6503 +
6504 +       if (unlikely(((mode & reqmode) == reqmode) && mode & GR_AUDITS)) {
6505 +               security_audit(GR_ACCESS_ACL_MSG, "successful",
6506 +                              gr_to_filename(dentry, mnt),
6507 +                              reqmode & GR_READ ? " reading" : "",
6508 +                              reqmode & GR_WRITE ? " writing" : "",
6509 +                              reqmode & GR_EXEC ? " executing" : "",
6510 +                              DEFAULTSECARGS);
6511 +               return reqmode;
6512 +       } else
6513 +           if (unlikely((mode & reqmode) != reqmode && !(mode & GR_SUPPRESS)))
6514 +       {
6515 +               security_alert(GR_ACCESS_ACL_MSG, "denied",
6516 +                              gr_to_filename(dentry, mnt),
6517 +                              reqmode & GR_READ ? " reading" : "",
6518 +                              reqmode & GR_WRITE ? " writing" : "",
6519 +                              reqmode & GR_EXEC ? " executing" : "",
6520 +                              DEFAULTSECARGS);
6521 +               return 0;
6522 +       } else if (unlikely((mode & reqmode) != reqmode))
6523 +               return 0;
6524 +
6525 +       return reqmode;
6526 +}
6527 +
6528 +#define generic_fs_handler(dentry, mnt, reqmode, fmt) \
6529 +{ \
6530 +       __u32 mode; \
6531 +       \
6532 +       mode = gr_search_file(dentry, reqmode | to_gr_audit(reqmode) | GR_SUPPRESS, mnt); \
6533 +       \
6534 +       if (unlikely(((mode & (reqmode)) == (reqmode)) && mode & GR_AUDITS)) { \
6535 +               security_audit(fmt, "successful", \
6536 +                               gr_to_filename(dentry, mnt), DEFAULTSECARGS); \
6537 +               return mode; \
6538 +       } else if (unlikely((mode & (reqmode)) != (reqmode) && !(mode & GR_SUPPRESS))) { \
6539 +               security_alert(fmt, "denied", gr_to_filename(dentry, mnt), \
6540 +                               DEFAULTSECARGS); \
6541 +               return 0; \
6542 +       } else if (unlikely((mode & (reqmode)) != (reqmode))) \
6543 +               return 0; \
6544 +       \
6545 +       return (reqmode); \
6546 +}
6547 +
6548 +__u32
6549 +gr_acl_handle_rmdir(const struct dentry * dentry, const struct vfsmount * mnt)
6550 +{
6551 +       generic_fs_handler(dentry, mnt, GR_WRITE | GR_DELETE , GR_RMDIR_ACL_MSG);
6552 +}
6553 +
6554 +__u32
6555 +gr_acl_handle_unlink(const struct dentry *dentry, const struct vfsmount *mnt)
6556 +{
6557 +       generic_fs_handler(dentry, mnt, GR_WRITE | GR_DELETE , GR_UNLINK_ACL_MSG);
6558 +}
6559 +
6560 +__u32
6561 +gr_acl_handle_truncate(const struct dentry *dentry, const struct vfsmount *mnt)
6562 +{
6563 +       generic_fs_handler(dentry, mnt, GR_WRITE, GR_TRUNCATE_ACL_MSG);
6564 +}
6565 +
6566 +__u32
6567 +gr_acl_handle_utime(const struct dentry *dentry, const struct vfsmount *mnt)
6568 +{
6569 +       generic_fs_handler(dentry, mnt, GR_WRITE, GR_ATIME_ACL_MSG);
6570 +}
6571 +
6572 +__u32
6573 +gr_acl_handle_fchmod(const struct dentry *dentry, const struct vfsmount *mnt,
6574 +                    mode_t mode)
6575 +{
6576 +       if (unlikely((mode != (mode_t)-1) && (mode & (S_ISUID | S_ISGID)))) {
6577 +               generic_fs_handler(dentry, mnt, GR_WRITE | GR_SETID,
6578 +                                  GR_FCHMOD_ACL_MSG);
6579 +       } else {
6580 +               generic_fs_handler(dentry, mnt, GR_WRITE, GR_FCHMOD_ACL_MSG);
6581 +       }
6582 +}
6583 +
6584 +__u32
6585 +gr_acl_handle_chmod(const struct dentry *dentry, const struct vfsmount *mnt,
6586 +                   mode_t mode)
6587 +{
6588 +       if (unlikely((mode != (mode_t)-1) && (mode & (S_ISUID | S_ISGID)))) {
6589 +               generic_fs_handler(dentry, mnt, GR_WRITE | GR_SETID,
6590 +                                  GR_CHMOD_ACL_MSG);
6591 +       } else {
6592 +               generic_fs_handler(dentry, mnt, GR_WRITE, GR_CHMOD_ACL_MSG);
6593 +       }
6594 +}
6595 +
6596 +__u32
6597 +gr_acl_handle_chown(const struct dentry *dentry, const struct vfsmount *mnt)
6598 +{
6599 +       generic_fs_handler(dentry, mnt, GR_WRITE, GR_CHOWN_ACL_MSG);
6600 +}
6601 +
6602 +__u32
6603 +gr_acl_handle_execve(const struct dentry *dentry, const struct vfsmount *mnt)
6604 +{
6605 +       generic_fs_handler(dentry, mnt, GR_EXEC, GR_EXEC_ACL_MSG);
6606 +}
6607 +
6608 +__u32
6609 +gr_acl_handle_unix(const struct dentry *dentry, const struct vfsmount *mnt)
6610 +{
6611 +       generic_fs_handler(dentry, mnt, GR_READ | GR_WRITE,
6612 +                          GR_UNIXCONNECT_ACL_MSG);
6613 +}
6614 +
6615 +__u32
6616 +gr_acl_handle_filldir(const struct dentry *dentry, const struct vfsmount *mnt,
6617 +                     const ino_t ino)
6618 +{
6619 +       if (likely((unsigned long)(dentry->d_inode))) {
6620 +               struct dentry d = *dentry;
6621 +               struct inode inode = *(dentry->d_inode);
6622 +
6623 +               inode.i_ino = ino;
6624 +               d.d_inode = &inode;
6625 +
6626 +               if (unlikely(!gr_search_file(&d, GR_FIND | GR_NOLEARN, mnt)))
6627 +                       return 0;
6628 +       }
6629 +
6630 +       return 1;
6631 +}
6632 +
6633 +__u32
6634 +gr_acl_handle_link(const struct dentry * new_dentry,
6635 +                  const struct dentry * parent_dentry,
6636 +                  const struct vfsmount * parent_mnt,
6637 +                  const struct dentry * old_dentry,
6638 +                  const struct vfsmount * old_mnt, const char *to)
6639 +{
6640 +       __u32 needmode = GR_WRITE | GR_CREATE;
6641 +       __u32 mode;
6642 +
6643 +       mode =
6644 +           gr_check_link(new_dentry, parent_dentry, parent_mnt, old_dentry,
6645 +                         old_mnt);
6646 +
6647 +       if (unlikely(((mode & needmode) == needmode) && mode & GR_AUDITS)) {
6648 +               security_audit(GR_LINK_ACL_MSG, "successful",
6649 +                              gr_to_filename(old_dentry, old_mnt), to,
6650 +                              DEFAULTSECARGS);
6651 +               return mode;
6652 +       } else if (unlikely(((mode & needmode) != needmode) && !(mode & GR_SUPPRESS))) {
6653 +               security_alert(GR_LINK_ACL_MSG, "denied",
6654 +                              gr_to_filename(old_dentry, old_mnt), to,
6655 +                              DEFAULTSECARGS);
6656 +               return 0;
6657 +       } else if (unlikely((mode & needmode) != needmode))
6658 +               return 0;
6659 +
6660 +       return (GR_WRITE | GR_CREATE);
6661 +}
6662 +
6663 +__u32
6664 +gr_acl_handle_symlink(const struct dentry * new_dentry,
6665 +                     const struct dentry * parent_dentry,
6666 +                     const struct vfsmount * parent_mnt, const char *from)
6667 +{
6668 +       __u32 needmode = GR_WRITE | GR_CREATE;
6669 +       __u32 mode;
6670 +
6671 +       mode =
6672 +           gr_check_create(new_dentry, parent_dentry, parent_mnt,
6673 +                           GR_CREATE | GR_AUDIT_CREATE |
6674 +                           GR_WRITE | GR_AUDIT_WRITE | GR_SUPPRESS);
6675 +
6676 +       if (unlikely(mode & GR_WRITE && mode & GR_AUDITS)) {
6677 +               security_audit(GR_SYMLINK_ACL_MSG, "successful",
6678 +                              from, gr_to_filename(new_dentry, parent_mnt),
6679 +                              DEFAULTSECARGS);
6680 +               return mode;
6681 +       } else if (unlikely(((mode & needmode) != needmode) && !(mode & GR_SUPPRESS))) {
6682 +               security_alert(GR_SYMLINK_ACL_MSG, "denied",
6683 +                              from, gr_to_filename(new_dentry, parent_mnt),
6684 +                              DEFAULTSECARGS);
6685 +               return 0;
6686 +       } else if (unlikely((mode & needmode) != needmode))
6687 +               return 0;
6688 +
6689 +       return (GR_WRITE | GR_CREATE);
6690 +}
6691 +
6692 +#define generic_fs_create_handler(new_dentry, parent_dentry, parent_mnt, reqmode, fmt) \
6693 +{ \
6694 +       __u32 mode; \
6695 +       \
6696 +       mode = gr_check_create(new_dentry, parent_dentry, parent_mnt, reqmode | to_gr_audit(reqmode) | GR_SUPPRESS); \
6697 +       \
6698 +       if (unlikely(((mode & (reqmode)) == (reqmode)) && mode & GR_AUDITS)) { \
6699 +               security_audit(fmt, "successful", \
6700 +                               gr_to_filename(new_dentry, parent_mnt), \
6701 +                               DEFAULTSECARGS); \
6702 +               return mode; \
6703 +       } else if (unlikely((mode & (reqmode)) != (reqmode) && !(mode & GR_SUPPRESS))) { \
6704 +               security_alert(fmt, "denied", \
6705 +                               gr_to_filename(new_dentry, parent_mnt), \
6706 +                               DEFAULTSECARGS); \
6707 +               return 0; \
6708 +       } else if (unlikely((mode & (reqmode)) != (reqmode))) \
6709 +               return 0; \
6710 +       \
6711 +       return (reqmode); \
6712 +}
6713 +
6714 +__u32
6715 +gr_acl_handle_mknod(const struct dentry * new_dentry,
6716 +                   const struct dentry * parent_dentry,
6717 +                   const struct vfsmount * parent_mnt,
6718 +                   const int mode)
6719 +{
6720 +       __u32 reqmode = GR_WRITE | GR_CREATE;
6721 +       if (unlikely(mode & (S_ISUID | S_ISGID)))
6722 +               reqmode |= GR_SETID;
6723 +
6724 +       generic_fs_create_handler(new_dentry, parent_dentry, parent_mnt,
6725 +                                 reqmode, GR_MKNOD_ACL_MSG);
6726 +}
6727 +
6728 +__u32
6729 +gr_acl_handle_mkdir(const struct dentry *new_dentry,
6730 +                   const struct dentry *parent_dentry,
6731 +                   const struct vfsmount *parent_mnt)
6732 +{
6733 +       generic_fs_create_handler(new_dentry, parent_dentry, parent_mnt,
6734 +                                 GR_WRITE | GR_CREATE, GR_MKDIR_ACL_MSG);
6735 +}
6736 +
6737 +#define RENAME_CHECK_SUCCESS(old, new) \
6738 +       (((old & (GR_WRITE | GR_READ)) == (GR_WRITE | GR_READ)) && \
6739 +        ((new & (GR_WRITE | GR_READ)) == (GR_WRITE | GR_READ)))
6740 +
6741 +int
6742 +gr_acl_handle_rename(struct dentry *new_dentry,
6743 +                    struct dentry *parent_dentry,
6744 +                    const struct vfsmount *parent_mnt,
6745 +                    struct dentry *old_dentry,
6746 +                    struct inode *old_parent_inode,
6747 +                    struct vfsmount *old_mnt, const char *newname)
6748 +{
6749 +       __u8 gr_replace = 1;
6750 +       __u32 comp1, comp2;
6751 +       int error = 0;
6752 +
6753 +       if (unlikely(!gr_acl_is_enabled()))
6754 +               return 1;
6755 +
6756 +       if (!new_dentry->d_inode) {
6757 +               gr_replace = 0;
6758 +
6759 +               comp1 = gr_check_create(new_dentry, parent_dentry, parent_mnt,
6760 +                                       GR_READ | GR_WRITE | GR_CREATE | GR_AUDIT_READ |
6761 +                                       GR_AUDIT_WRITE | GR_AUDIT_CREATE | GR_SUPPRESS);
6762 +               comp2 = gr_search_file(old_dentry, GR_READ | GR_WRITE |
6763 +                                      GR_DELETE | GR_AUDIT_DELETE |
6764 +                                      GR_AUDIT_READ | GR_AUDIT_WRITE |
6765 +                                      GR_SUPPRESS, old_mnt);
6766 +       } else {
6767 +               comp1 = gr_search_file(new_dentry, GR_READ | GR_WRITE |
6768 +                                      GR_CREATE | GR_DELETE |
6769 +                                      GR_AUDIT_CREATE | GR_AUDIT_DELETE |
6770 +                                      GR_AUDIT_READ | GR_AUDIT_WRITE |
6771 +                                      GR_SUPPRESS, parent_mnt);
6772 +               comp2 =
6773 +                   gr_search_file(old_dentry,
6774 +                                  GR_READ | GR_WRITE | GR_AUDIT_READ |
6775 +                                  GR_DELETE | GR_AUDIT_DELETE |
6776 +                                  GR_AUDIT_WRITE | GR_SUPPRESS, old_mnt);
6777 +       }
6778 +
6779 +       if (RENAME_CHECK_SUCCESS(comp1, comp2) &&
6780 +           ((comp1 & GR_AUDITS) || (comp2 & GR_AUDITS)))
6781 +               security_audit(GR_RENAME_ACL_MSG, "successful",
6782 +                              gr_to_filename(old_dentry, old_mnt),
6783 +                              newname, DEFAULTSECARGS);
6784 +       else if (!RENAME_CHECK_SUCCESS(comp1, comp2) && !(comp1 & GR_SUPPRESS)
6785 +                && !(comp2 & GR_SUPPRESS)) {
6786 +               security_alert(GR_RENAME_ACL_MSG, "denied",
6787 +                              gr_to_filename(old_dentry, old_mnt), newname,
6788 +                              DEFAULTSECARGS);
6789 +               error = -EACCES;
6790 +       } else if (unlikely(!RENAME_CHECK_SUCCESS(comp1, comp2)))
6791 +               error = -EACCES;
6792 +
6793 +       if (error)
6794 +               return error;
6795 +
6796 +       error = gr_handle_rename(old_parent_inode, parent_dentry->d_inode,
6797 +                                old_dentry, new_dentry, old_mnt, gr_replace);
6798 +
6799 +       return error;
6800 +}
6801 +
6802 +void
6803 +gr_acl_handle_exit(void)
6804 +{
6805 +       u16 id;
6806 +       char *rolename;
6807 +
6808 +       if (unlikely(current->acl_sp_role && gr_acl_is_enabled())) {
6809 +               id = current->acl_role_id;
6810 +               rolename = current->role->rolename;
6811 +               gr_set_acls(1);
6812 +               security_alert_good(GR_SPROLEL_ACL_MSG,
6813 +                                   rolename, id, DEFAULTSECARGS);
6814 +       }
6815 +
6816 +       if (current->exec_file) {
6817 +               fput(current->exec_file);
6818 +               current->exec_file = NULL;
6819 +       }
6820 +}
6821 +
6822 +int
6823 +gr_acl_handle_procpidmem(const struct task_struct *task)
6824 +{
6825 +       if (unlikely(!gr_acl_is_enabled()))
6826 +               return 0;
6827 +
6828 +       if (task->acl->mode & GR_PROTPROCFD)
6829 +               return -EACCES;
6830 +
6831 +       return 0;
6832 +}
6833 diff -urN linux-2.4.21/grsecurity/gracl_ip.c linux-2.4.21/grsecurity/gracl_ip.c
6834 --- linux-2.4.21/grsecurity/gracl_ip.c  1969-12-31 19:00:00.000000000 -0500
6835 +++ linux-2.4.21/grsecurity/gracl_ip.c  2003-07-29 17:58:19.000000000 -0400
6836 @@ -0,0 +1,235 @@
6837 +/* 
6838 + * grsecurity/gracl_ip.c
6839 + * Copyright Brad Spengler 2002, 2003
6840 + *
6841 + */
6842 +
6843 +#include <linux/kernel.h>
6844 +#include <asm/uaccess.h>
6845 +#include <asm/errno.h>
6846 +#include <net/sock.h>
6847 +#include <linux/file.h>
6848 +#include <linux/fs.h>
6849 +#include <linux/net.h>
6850 +#include <linux/in.h>
6851 +#include <linux/skbuff.h>
6852 +#include <linux/ip.h>
6853 +#include <linux/udp.h>
6854 +#include <linux/smp_lock.h>
6855 +#include <linux/types.h>
6856 +#include <linux/sched.h>
6857 +#include <linux/gracl.h>
6858 +#include <linux/grsecurity.h>
6859 +#include <linux/grinternal.h>
6860 +
6861 +#define GR_BIND        0x01
6862 +#define GR_CONNECT     0x02
6863 +
6864 +static const char * gr_protocols[256] = {
6865 +       "ip", "icmp", "igmp", "ggp", "ipencap", "st", "tcp", "cbt",
6866 +       "egp", "igp", "bbn-rcc", "nvp", "pup", "argus", "emcon", "xnet",
6867 +       "chaos", "udp", "mux", "dcn", "hmp", "prm", "xns-idp", "trunk-1",
6868 +       "trunk-2", "leaf-1", "leaf-2", "rdp", "irtp", "iso-tp4", "netblt", "mfe-nsp",
6869 +       "merit-inp", "sep", "3pc", "idpr", "xtp", "ddp", "idpr-cmtp", "tp++",
6870 +       "il", "ipv6", "sdrp", "ipv6-route", "ipv6-frag", "idrp", "rsvp", "gre",
6871 +       "mhrp", "bna", "ipv6-crypt", "ipv6-auth", "i-nlsp", "swipe", "narp", "mobile",
6872 +       "tlsp", "skip", "ipv6-icmp", "ipv6-nonxt", "ipv6-opts", "unknown:61", "cftp", "unknown:63",
6873 +       "sat-expak", "kryptolan", "rvd", "ippc", "unknown:68", "sat-mon", "visa", "ipcv",
6874 +       "cpnx", "cphb", "wsn", "pvp", "br-sat-mon", "sun-nd", "wb-mon", "wb-expak", 
6875 +       "iso-ip", "vmtp", "secure-vmtp", "vines", "ttp", "nfsnet-igp", "dgp", "tcf", 
6876 +       "eigrp", "ospf", "sprite-rpc", "larp", "mtp", "ax.25", "ipip", "micp",
6877 +       "scc-sp", "etherip", "encap", "unknown:99", "gmtp", "ifmp", "pnni", "pim",
6878 +       "aris", "scps", "qnx", "a/n", "ipcomp", "snp", "compaq-peer", "ipx-in-ip",
6879 +       "vrrp", "pgm", "unknown:114", "l2tp", "ddx", "iatp", "stp", "srp",
6880 +       "uti", "smp", "sm", "ptp", "isis", "fire", "crtp", "crdup",
6881 +       "sscopmce", "iplt", "sps", "pipe", "sctp", "fc", "unkown:134", "unknown:135",
6882 +       "unknown:136", "unknown:137", "unknown:138", "unknown:139", "unknown:140", "unknown:141", "unknown:142", "unknown:143",
6883 +       "unknown:144", "unknown:145", "unknown:146", "unknown:147", "unknown:148", "unknown:149", "unknown:150", "unknown:151",
6884 +       "unknown:152", "unknown:153", "unknown:154", "unknown:155", "unknown:156", "unknown:157", "unknown:158", "unknown:159",
6885 +       "unknown:160", "unknown:161", "unknown:162", "unknown:163", "unknown:164", "unknown:165", "unknown:166", "unknown:167",
6886 +       "unknown:168", "unknown:169", "unknown:170", "unknown:171", "unknown:172", "unknown:173", "unknown:174", "unknown:175",
6887 +       "unknown:176", "unknown:177", "unknown:178", "unknown:179", "unknown:180", "unknown:181", "unknown:182", "unknown:183",
6888 +       "unknown:184", "unknown:185", "unknown:186", "unknown:187", "unknown:188", "unknown:189", "unknown:190", "unknown:191",
6889 +       "unknown:192", "unknown:193", "unknown:194", "unknown:195", "unknown:196", "unknown:197", "unknown:198", "unknown:199",
6890 +       "unknown:200", "unknown:201", "unknown:202", "unknown:203", "unknown:204", "unknown:205", "unknown:206", "unknown:207",
6891 +       "unknown:208", "unknown:209", "unknown:210", "unknown:211", "unknown:212", "unknown:213", "unknown:214", "unknown:215",
6892 +       "unknown:216", "unknown:217", "unknown:218", "unknown:219", "unknown:220", "unknown:221", "unknown:222", "unknown:223",
6893 +       "unknown:224", "unknown:225", "unknown:226", "unknown:227", "unknown:228", "unknown:229", "unknown:230", "unknown:231",
6894 +       "unknown:232", "unknown:233", "unknown:234", "unknown:235", "unknown:236", "unknown:237", "unknown:238", "unknown:239",
6895 +       "unknown:240", "unknown:241", "unknown:242", "unknown:243", "unknown:244", "unknown:245", "unknown:246", "unknown:247",
6896 +       "unknown:248", "unknown:249", "unknown:250", "unknown:251", "unknown:252", "unknown:253", "unknown:254", "unknown:255",
6897 +       };
6898 +
6899 +static const char * gr_socktypes[11] = {
6900 +       "unknown:0", "stream", "dgram", "raw", "rdm", "seqpacket", "unknown:6", 
6901 +       "unknown:7", "unknown:8", "unknown:9", "packet"
6902 +       };
6903 +
6904 +static __inline__ const char *
6905 +gr_proto_to_name(unsigned char proto)
6906 +{
6907 +       return gr_protocols[proto];
6908 +}
6909 +
6910 +static __inline__ const char *
6911 +gr_socktype_to_name(unsigned char type)
6912 +{
6913 +       return gr_socktypes[type];
6914 +}
6915 +
6916 +int
6917 +gr_search_socket(const int domain, const int type, const int protocol)
6918 +{
6919 +       struct acl_subject_label *curr;
6920 +
6921 +       if (unlikely(!gr_acl_is_enabled()))
6922 +               goto exit;
6923 +
6924 +       if ((domain < 0) || (type < 0) || (protocol < 0) || (domain != PF_INET)
6925 +           || (domain >= NPROTO) || (type >= SOCK_MAX) || (protocol > 255))
6926 +               goto exit;      // let the kernel handle it
6927 +
6928 +       curr = current->acl;
6929 +
6930 +       if (!curr->ips)
6931 +               goto exit;
6932 +
6933 +       if ((curr->ip_type & (1 << type)) &&
6934 +           (curr->ip_proto[protocol / 32] & (1 << (protocol % 32))))
6935 +               goto exit;
6936 +
6937 +       if (curr->mode & GR_LEARN) {
6938 +               /* we don't place acls on raw sockets , and sometimes
6939 +                  dgram/ip sockets are opened for ioctl and not
6940 +                  bind/connect, so we'll fake a bind learn log */
6941 +               if (type == SOCK_RAW || type == SOCK_PACKET) {
6942 +                       __u32 fakeip = 0;
6943 +                       security_learn(GR_IP_LEARN_MSG, current->role->rolename,
6944 +                                      current->role->roletype, current->uid,
6945 +                                      current->gid, current->exec_file ?
6946 +                                      gr_to_filename(current->exec_file->f_dentry,
6947 +                                      current->exec_file->f_vfsmnt) :
6948 +                                      curr->filename, curr->filename,
6949 +                                      NIPQUAD(fakeip), 0, type,
6950 +                                      protocol, GR_CONNECT, NIPQUAD(current->curr_ip));
6951 +               } else if ((type == SOCK_DGRAM) && (protocol == IPPROTO_IP)) {
6952 +                       __u32 fakeip = 0;
6953 +                       security_learn(GR_IP_LEARN_MSG, current->role->rolename,
6954 +                                      current->role->roletype, current->uid,
6955 +                                      current->gid, current->exec_file ?
6956 +                                      gr_to_filename(current->exec_file->f_dentry,
6957 +                                      current->exec_file->f_vfsmnt) :
6958 +                                      curr->filename, curr->filename,
6959 +                                      NIPQUAD(fakeip), 0, type,
6960 +                                      protocol, GR_BIND, NIPQUAD(current->curr_ip));
6961 +               }
6962 +               /* we'll log when they use connect or bind */
6963 +               goto exit;
6964 +       }
6965 +
6966 +       security_alert(GR_SOCK_MSG, "inet", gr_socktype_to_name(type),
6967 +                      gr_proto_to_name(protocol), DEFAULTSECARGS);
6968 +
6969 +       return 0;
6970 +      exit:
6971 +       return 1;
6972 +}
6973 +
6974 +static __inline__ int
6975 +gr_search_connectbind(const int mode, const struct sock *sk,
6976 +                     const struct sockaddr_in *addr, const int type)
6977 +{
6978 +       struct acl_subject_label *curr;
6979 +       struct acl_ip_label *ip;
6980 +       unsigned long i;
6981 +       __u32 ip_addr = 0;
6982 +       __u16 ip_port = 0;
6983 +
6984 +       if (unlikely(!gr_acl_is_enabled() || sk->family != PF_INET))
6985 +               return 1;
6986 +
6987 +       curr = current->acl;
6988 +
6989 +       if (!curr->ips)
6990 +               return 1;
6991 +
6992 +       ip_addr = addr->sin_addr.s_addr;
6993 +       ip_port = ntohs(addr->sin_port);
6994 +
6995 +       for (i = 0; i < curr->ip_num; i++) {
6996 +               ip = *(curr->ips + i);
6997 +               if ((ip->mode & mode) &&
6998 +                   (ip_port >= ip->low) &&
6999 +                   (ip_port <= ip->high) &&
7000 +                   ((ntohl(ip_addr) & ip->netmask) ==
7001 +                    (ntohl(ip->addr) & ip->netmask))
7002 +                   && (ip->
7003 +                       proto[sk->protocol / 32] & (1 << (sk->protocol % 32)))
7004 +                   && (ip->type & (1 << type)))
7005 +                       return 1;
7006 +       }
7007 +
7008 +       if (curr->mode & GR_LEARN) {
7009 +               security_learn(GR_IP_LEARN_MSG, current->role->rolename,
7010 +                              current->role->roletype, current->uid,
7011 +                              current->gid, current->exec_file ?
7012 +                              gr_to_filename(current->exec_file->f_dentry,
7013 +                              current->exec_file->f_vfsmnt) :
7014 +                              curr->filename, curr->filename,
7015 +                              NIPQUAD(ip_addr), ip_port, type,
7016 +                              sk->protocol, mode, NIPQUAD(current->curr_ip));
7017 +               return 1;
7018 +       }
7019 +
7020 +       if (mode == GR_BIND)
7021 +               security_alert(GR_BIND_ACL_MSG, NIPQUAD(ip_addr), ip_port,
7022 +                              gr_socktype_to_name(type), gr_proto_to_name(sk->protocol),
7023 +                              DEFAULTSECARGS);
7024 +       else if (mode == GR_CONNECT)
7025 +               security_alert(GR_CONNECT_ACL_MSG, NIPQUAD(ip_addr), ip_port,
7026 +                              gr_socktype_to_name(type), gr_proto_to_name(sk->protocol),
7027 +                              DEFAULTSECARGS);
7028 +
7029 +       return 0;
7030 +}
7031 +
7032 +int
7033 +gr_search_connect(const struct socket *sock, const struct sockaddr_in *addr)
7034 +{
7035 +       return gr_search_connectbind(GR_CONNECT, sock->sk, addr, sock->type);
7036 +}
7037 +
7038 +int
7039 +gr_search_bind(const struct socket *sock, const struct sockaddr_in *addr)
7040 +{
7041 +       return gr_search_connectbind(GR_BIND, sock->sk, addr, sock->type);
7042 +}
7043 +
7044 +int
7045 +gr_search_udp_sendmsg(const struct sock *sk, const struct sockaddr_in *addr)
7046 +{
7047 +       if (addr)
7048 +               return gr_search_connectbind(GR_CONNECT, sk, addr, SOCK_DGRAM);
7049 +       else {
7050 +               struct sockaddr_in sin;
7051 +
7052 +               sin.sin_addr.s_addr = sk->daddr;
7053 +               sin.sin_port = sk->dport;
7054 +
7055 +               return gr_search_connectbind(GR_CONNECT, sk, &sin, SOCK_DGRAM);
7056 +       }
7057 +}
7058 +
7059 +int
7060 +gr_search_udp_recvmsg(const struct sock *sk, const struct sk_buff *skb)
7061 +{
7062 +       struct sockaddr_in sin;
7063 +
7064 +       if (unlikely(skb->len < sizeof (struct udphdr)))
7065 +               return 1;       // skip this packet
7066 +
7067 +       sin.sin_addr.s_addr = skb->nh.iph->saddr;
7068 +       sin.sin_port = skb->h.uh->source;
7069 +
7070 +       return gr_search_connectbind(GR_CONNECT, sk, &sin, SOCK_DGRAM);
7071 +}
7072 diff -urN linux-2.4.21/grsecurity/gracl_learn.c linux-2.4.21/grsecurity/gracl_learn.c
7073 --- linux-2.4.21/grsecurity/gracl_learn.c       1969-12-31 19:00:00.000000000 -0500
7074 +++ linux-2.4.21/grsecurity/gracl_learn.c       2003-07-29 17:58:19.000000000 -0400
7075 @@ -0,0 +1,228 @@
7076 +#include <linux/kernel.h>
7077 +#include <linux/mm.h>
7078 +#include <linux/sched.h>
7079 +#include <linux/poll.h>
7080 +#include <linux/smp_lock.h>
7081 +#include <linux/string.h>
7082 +#include <linux/file.h>
7083 +#include <linux/types.h>
7084 +#include <linux/vmalloc.h>
7085 +#include <linux/grinternal.h>
7086 +
7087 +extern ssize_t write_grsec_handler(struct file * file, const char * buf,
7088 +                                  size_t count, loff_t *ppos);
7089 +extern int gr_acl_is_enabled(void);
7090 +
7091 +static DECLARE_WAIT_QUEUE_HEAD(learn_wait);
7092 +static DECLARE_WAIT_QUEUE_HEAD(input_wait);
7093 +static atomic_t learn_buffer_count = ATOMIC_INIT(0);
7094 +static int gr_learn_attached;
7095 +
7096 +#define LEARN_BUFFER_SLOTS 256
7097 +#define LEARN_BUFFER_SIZE 16384
7098 +
7099 +static spinlock_t learn_buffer_lock[LEARN_BUFFER_SLOTS] = { [0 ... (LEARN_BUFFER_SLOTS - 1)] = SPIN_LOCK_UNLOCKED };
7100 +static char *learn_buffer[LEARN_BUFFER_SLOTS];
7101 +static int learn_buffer_len[LEARN_BUFFER_SLOTS];
7102 +
7103 +static ssize_t
7104 +read_learn(struct file *file, char * buf, size_t count, loff_t * ppos)
7105 +{
7106 +       DECLARE_WAITQUEUE(wait, current);
7107 +       ssize_t retval = 0;
7108 +       char *tmp;
7109 +       unsigned int len;
7110 +       int i;
7111 +
7112 +       add_wait_queue(&learn_wait, &wait);
7113 +       set_current_state(TASK_INTERRUPTIBLE);
7114 +       do {
7115 +               if (atomic_read(&learn_buffer_count) > 1)
7116 +                       break;
7117 +
7118 +               if (file->f_flags & O_NONBLOCK) {
7119 +                       retval = -EAGAIN;
7120 +                       goto out;
7121 +               }
7122 +               if (signal_pending(current)) {
7123 +                       retval = -ERESTARTSYS;
7124 +                       goto out;
7125 +               }
7126 +
7127 +               schedule();
7128 +       } while (1);
7129 +
7130 +
7131 +       for (i = 0; i < LEARN_BUFFER_SLOTS; i++) {
7132 +               spin_lock(&learn_buffer_lock[i]);
7133 +               len = learn_buffer_len[i];
7134 +               tmp = learn_buffer[i];
7135 +               if (!len || !tmp) {
7136 +                       spin_unlock(&learn_buffer_lock[i]);
7137 +                       continue;
7138 +               }
7139 +               learn_buffer[i] = NULL;
7140 +               learn_buffer_len[i] = 0;
7141 +               spin_unlock(&learn_buffer_lock[i]);
7142 +
7143 +               if (count < ((i * LEARN_BUFFER_SIZE) + len)) {
7144 +                       retval = -EINVAL;
7145 +                       vfree(tmp);
7146 +                       goto out;
7147 +               }
7148 +               if (copy_to_user(buf + (i * LEARN_BUFFER_SIZE), tmp, len)) {
7149 +                       retval = -EFAULT;
7150 +                       vfree(tmp);
7151 +                       goto out;
7152 +               }
7153 +
7154 +               retval += len;
7155 +               vfree(tmp);
7156 +               atomic_dec(&learn_buffer_count);
7157 +               atomic_dec(&learn_buffer_count);
7158 +       }
7159 +
7160 +       wake_up(&input_wait);
7161 +out:
7162 +       set_current_state(TASK_RUNNING);
7163 +       remove_wait_queue(&learn_wait, &wait);
7164 +       return retval;
7165 +}
7166 +
7167 +static unsigned int
7168 +poll_learn(struct file * file, poll_table * wait)
7169 +{
7170 +       poll_wait(file, &learn_wait, wait);
7171 +
7172 +       if (atomic_read(&learn_buffer_count) > 1)
7173 +               return (POLLIN | POLLRDNORM);
7174 +
7175 +       return 0;
7176 +}
7177 +
7178 +void
7179 +gr_clear_learn_entries(void)
7180 +{
7181 +       int i;
7182 +
7183 +       atomic_set(&learn_buffer_count, 0);
7184 +       wake_up(&input_wait);
7185 +       
7186 +       for (i = 0; i < LEARN_BUFFER_SLOTS; i++) {
7187 +               if (learn_buffer_len[i]) {
7188 +                       vfree(learn_buffer[i]);
7189 +                       learn_buffer[i] = NULL;
7190 +                       learn_buffer_len[i] = 0;
7191 +               }
7192 +       }
7193 +
7194 +       return;
7195 +}
7196 +
7197 +void
7198 +gr_add_learn_entry(const char *fmt, ...)
7199 +{
7200 +       DECLARE_WAITQUEUE(wait, current);
7201 +       va_list args;
7202 +       char *tmpbuf;
7203 +       char *buf;
7204 +       int i;
7205 +       unsigned int len;
7206 +
7207 +       if (!gr_learn_attached)
7208 +               return;
7209 +
7210 +       tmpbuf = vmalloc(LEARN_BUFFER_SIZE);
7211 +
7212 +       if (tmpbuf == NULL)
7213 +               return;
7214 +
7215 +       va_start(args, fmt);
7216 +       len = vsnprintf(tmpbuf, LEARN_BUFFER_SIZE, fmt, args);
7217 +       va_end(args);
7218 +
7219 +       if (len < 0)
7220 +               len = LEARN_BUFFER_SIZE - 1;
7221 +
7222 +       buf = vmalloc(len + 1);
7223 +
7224 +       if (buf == NULL) {
7225 +               vfree(tmpbuf);
7226 +               return;
7227 +       }
7228 +
7229 +       memcpy(buf, tmpbuf, len);
7230 +       buf[len] = '\0';
7231 +       vfree(tmpbuf);
7232 +
7233 +       add_wait_queue(&input_wait, &wait);
7234 +
7235 +       atomic_inc(&learn_buffer_count);
7236 +       while (atomic_read(&learn_buffer_count) > ((2 * (LEARN_BUFFER_SLOTS - 1)) + 1)) {
7237 +               /* don't sleep under the BKL */
7238 +               if (unlikely(current->lock_depth >= 0)) {
7239 +                       remove_wait_queue(&input_wait, &wait);
7240 +                       atomic_dec(&learn_buffer_count);
7241 +                       vfree(buf);
7242 +                       return;
7243 +               }
7244 +               sleep_on(&input_wait);
7245 +       }
7246 +
7247 +       if (!gr_acl_is_enabled()) {
7248 +               remove_wait_queue(&input_wait, &wait);
7249 +               atomic_dec(&learn_buffer_count);
7250 +               vfree(buf);
7251 +               return;
7252 +       }
7253 +
7254 +       for (i = 0; i < LEARN_BUFFER_SLOTS; i++) {
7255 +               spin_lock(&learn_buffer_lock[i]);
7256 +
7257 +               if (learn_buffer_len[i]) {
7258 +                       spin_unlock(&learn_buffer_lock[i]);
7259 +                       continue;
7260 +               }
7261 +
7262 +               learn_buffer[i] = buf;
7263 +
7264 +               learn_buffer_len[i] = len + 1;
7265 +
7266 +               atomic_inc(&learn_buffer_count);
7267 +               spin_unlock(&learn_buffer_lock[i]);
7268 +               break;
7269 +       }
7270 +
7271 +       remove_wait_queue(&input_wait, &wait);
7272 +       wake_up_interruptible(&learn_wait);
7273 +
7274 +       return;
7275 +}
7276 +
7277 +static int
7278 +open_learn(struct inode *inode, struct file *file)
7279 +{
7280 +       if (file->f_mode & FMODE_READ && gr_learn_attached)
7281 +               return -EBUSY;
7282 +       else if (file->f_mode & FMODE_READ)
7283 +               gr_learn_attached = 1;
7284 +
7285 +       return 0;
7286 +}
7287 +
7288 +static int
7289 +close_learn(struct inode *inode, struct file *file)
7290 +{
7291 +       if (file->f_mode & FMODE_READ)
7292 +               gr_learn_attached = 0;
7293 +
7294 +       return 0;
7295 +}
7296 +               
7297 +struct file_operations grsec_fops = {
7298 +       read:           read_learn,
7299 +       write:          write_grsec_handler,
7300 +       open:           open_learn,
7301 +       release:        close_learn,
7302 +       poll:           poll_learn,
7303 +};
7304 diff -urN linux-2.4.21/grsecurity/gracl_res.c linux-2.4.21/grsecurity/gracl_res.c
7305 --- linux-2.4.21/grsecurity/gracl_res.c 1969-12-31 19:00:00.000000000 -0500
7306 +++ linux-2.4.21/grsecurity/gracl_res.c 2003-07-29 17:58:19.000000000 -0400
7307 @@ -0,0 +1,42 @@
7308 +/* resource handling routines (c) Brad Spengler 2002, 2003 */
7309 +
7310 +#include <linux/kernel.h>
7311 +#include <linux/sched.h>
7312 +#include <linux/gracl.h>
7313 +#include <linux/grinternal.h>
7314 +
7315 +static const char *restab_log[11] = {
7316 +       "RLIMIT_CPU",
7317 +       "RLIMIT_FSIZE",
7318 +       "RLIMIT_DATA",
7319 +       "RLIMIT_STACK",
7320 +       "RLIMIT_CORE",
7321 +       "RLIMIT_RSS",
7322 +       "RLIMIT_NPROC",
7323 +       "RLIMIT_NOFILE",
7324 +       "RLIMIT_MEMLOCK",
7325 +       "RLIMIT_AS",
7326 +       "RLIMIT_LOCKS"
7327 +};
7328 +
7329 +__inline__ void
7330 +gr_log_resource(const struct task_struct *task,
7331 +               const int res, const unsigned long wanted)
7332 +{
7333 +       if (unlikely(res == RLIMIT_NPROC && 
7334 +           (cap_raised(task->cap_effective, CAP_SYS_ADMIN) || 
7335 +            cap_raised(task->cap_effective, CAP_SYS_RESOURCE))))
7336 +               return;
7337 +
7338 +       if (unlikely(wanted >= task->rlim[res].rlim_cur &&
7339 +                    task->rlim[res].rlim_cur != RLIM_INFINITY))
7340 +               security_alert(GR_RESOURCE_MSG, wanted, restab_log[res],
7341 +                              task->rlim[res].rlim_cur, task->comm,
7342 +                              task->pid, task->uid, task->euid,
7343 +                              task->gid, task->egid, task->p_pptr->comm,
7344 +                              task->p_pptr->pid, task->p_pptr->uid, 
7345 +                              task->p_pptr->euid, task->p_pptr->gid,
7346 +                              task->p_pptr->egid);
7347 +
7348 +       return;
7349 +}
7350 diff -urN linux-2.4.21/grsecurity/gracl_segv.c linux-2.4.21/grsecurity/gracl_segv.c
7351 --- linux-2.4.21/grsecurity/gracl_segv.c        1969-12-31 19:00:00.000000000 -0500
7352 +++ linux-2.4.21/grsecurity/gracl_segv.c        2003-06-23 11:49:16.000000000 -0400
7353 @@ -0,0 +1,323 @@
7354 +/* 
7355 + * grsecurity/gracl_segv.c
7356 + * Copyright Brad Spengler 2002, 2003
7357 + *
7358 + */
7359 +
7360 +#include <linux/kernel.h>
7361 +#include <linux/mm.h>
7362 +#include <asm/uaccess.h>
7363 +#include <asm/errno.h>
7364 +#include <asm/mman.h>
7365 +#include <net/sock.h>
7366 +#include <linux/file.h>
7367 +#include <linux/fs.h>
7368 +#include <linux/net.h>
7369 +#include <linux/in.h>
7370 +#include <linux/smp_lock.h>
7371 +#include <linux/slab.h>
7372 +#include <linux/types.h>
7373 +#include <linux/sched.h>
7374 +#include <linux/timer.h>
7375 +#include <linux/gracl.h>
7376 +#include <linux/grsecurity.h>
7377 +#include <linux/grinternal.h>
7378 +
7379 +static struct crash_uid *uid_set;
7380 +static unsigned short uid_used;
7381 +static rwlock_t gr_uid_lock = RW_LOCK_UNLOCKED;
7382 +extern rwlock_t gr_inode_lock;
7383 +extern __inline__ struct acl_subject_label *lookup_acl_subj_label(const ino_t
7384 +                                                                 inode,
7385 +                                                                 const kdev_t
7386 +                                                                 dev,
7387 +                                                                 struct
7388 +                                                                 acl_role_label
7389 +                                                                 *role);
7390 +
7391 +int
7392 +gr_init_uidset(void)
7393 +{
7394 +       uid_set =
7395 +           kmalloc(GR_UIDTABLE_MAX * sizeof (struct crash_uid), GFP_KERNEL);
7396 +       uid_used = 0;
7397 +
7398 +       return uid_set ? 1 : 0;
7399 +}
7400 +
7401 +void
7402 +gr_free_uidset(void)
7403 +{
7404 +       if (uid_set)
7405 +               kfree(uid_set);
7406 +
7407 +       return;
7408 +}
7409 +
7410 +int
7411 +gr_find_uid(const uid_t uid)
7412 +{
7413 +       struct crash_uid *tmp = uid_set;
7414 +       uid_t buid;
7415 +       int low = 0, high = uid_used - 1, mid;
7416 +
7417 +       while (high >= low) {
7418 +               mid = (low + high) >> 1;
7419 +               buid = tmp[mid].uid;
7420 +               if (buid == uid)
7421 +                       return mid;
7422 +               if (buid > uid)
7423 +                       high = mid - 1;
7424 +               if (buid < uid)
7425 +                       low = mid + 1;
7426 +       }
7427 +
7428 +       return -1;
7429 +}
7430 +
7431 +static __inline__ void
7432 +gr_insertsort(void)
7433 +{
7434 +       unsigned short i, j;
7435 +       struct crash_uid index;
7436 +
7437 +       for (i = 1; i < uid_used; i++) {
7438 +               index = uid_set[i];
7439 +               j = i;
7440 +               while ((j > 0) && uid_set[j - 1].uid > index.uid) {
7441 +                       uid_set[j] = uid_set[j - 1];
7442 +                       j--;
7443 +               }
7444 +               uid_set[j] = index;
7445 +       }
7446 +
7447 +       return;
7448 +}
7449 +
7450 +static __inline__ void
7451 +gr_insert_uid(const uid_t uid, const unsigned long expires)
7452 +{
7453 +       int loc;
7454 +
7455 +       if (uid_used == GR_UIDTABLE_MAX)
7456 +               return;
7457 +
7458 +       loc = gr_find_uid(uid);
7459 +
7460 +       if (loc >= 0) {
7461 +               uid_set[loc].expires = expires;
7462 +               return;
7463 +       }
7464 +
7465 +       uid_set[uid_used].uid = uid;
7466 +       uid_set[uid_used].expires = expires;
7467 +       uid_used++;
7468 +
7469 +       gr_insertsort();
7470 +
7471 +       return;
7472 +}
7473 +
7474 +void
7475 +gr_remove_uid(const unsigned short loc)
7476 +{
7477 +       unsigned short i;
7478 +
7479 +       for (i = loc + 1; i < uid_used; i++)
7480 +               uid_set[i - i] = uid_set[i];
7481 +
7482 +       uid_used--;
7483 +
7484 +       return;
7485 +}
7486 +
7487 +int
7488 +gr_check_crash_uid(const uid_t uid)
7489 +{
7490 +       int loc;
7491 +
7492 +       if (unlikely(!gr_acl_is_enabled()))
7493 +               return 0;
7494 +
7495 +       read_lock(&gr_uid_lock);
7496 +       loc = gr_find_uid(uid);
7497 +       read_unlock(&gr_uid_lock);
7498 +
7499 +       if (loc < 0)
7500 +               return 0;
7501 +
7502 +       write_lock(&gr_uid_lock);
7503 +       if (time_before_eq(uid_set[loc].expires, jiffies))
7504 +               gr_remove_uid(loc);
7505 +       else {
7506 +               write_unlock(&gr_uid_lock);
7507 +               return 1;
7508 +       }
7509 +
7510 +       write_unlock(&gr_uid_lock);
7511 +       return 0;
7512 +}
7513 +
7514 +static __inline__ int
7515 +proc_is_setxid(const struct task_struct *task)
7516 +{
7517 +       if (task->uid != task->euid || task->uid != task->suid ||
7518 +           task->uid != task->fsuid)
7519 +               return 1;
7520 +       if (task->gid != task->egid || task->gid != task->sgid ||
7521 +           task->gid != task->fsgid)
7522 +               return 1;
7523 +
7524 +       return 0;
7525 +}
7526 +static __inline__ int
7527 +gr_fake_force_sig(int sig, struct task_struct *t)
7528 +{
7529 +       unsigned long int flags;
7530 +
7531 +       spin_lock_irqsave(&t->sigmask_lock, flags);
7532 +       if (t->sig == NULL) {
7533 +               spin_unlock_irqrestore(&t->sigmask_lock, flags);
7534 +               return -ESRCH;
7535 +       }
7536 +
7537 +       if (t->sig->action[sig - 1].sa.sa_handler == SIG_IGN)
7538 +               t->sig->action[sig - 1].sa.sa_handler = SIG_DFL;
7539 +       sigdelset(&t->blocked, sig);
7540 +       recalc_sigpending(t);
7541 +       spin_unlock_irqrestore(&t->sigmask_lock, flags);
7542 +
7543 +       return send_sig_info(sig, (void *) 1L, t);
7544 +}
7545 +
7546 +void
7547 +gr_handle_crash(struct task_struct *task, const int sig)
7548 +{
7549 +       struct acl_subject_label *curr;
7550 +       struct acl_subject_label *curr2;
7551 +       struct task_struct *tsk;
7552 +
7553 +       if (sig != SIGSEGV && sig != SIGKILL && sig != SIGBUS && sig != SIGILL)
7554 +               return;
7555 +
7556 +       if (unlikely(!gr_acl_is_enabled()))
7557 +               return;
7558 +
7559 +       curr = task->acl;
7560 +
7561 +       if (!(curr->resmask & (1 << GR_CRASH_RES)))
7562 +               return;
7563 +
7564 +       if (time_before_eq(curr->expires, jiffies)) {
7565 +               curr->expires = 0;
7566 +               curr->crashes = 0;
7567 +       }
7568 +
7569 +       curr->crashes++;
7570 +
7571 +       if (!curr->expires)
7572 +               curr->expires = jiffies + curr->res[GR_CRASH_RES].rlim_max;
7573 +
7574 +       if ((curr->crashes >= curr->res[GR_CRASH_RES].rlim_cur) &&
7575 +           time_after(curr->expires, jiffies)) {
7576 +               if (task->uid && proc_is_setxid(task)) {
7577 +                       security_alert(GR_SEGVSTART_ACL_MSG, task->comm,
7578 +                                      task->pid, task->uid, task->euid,
7579 +                                      task->gid, task->egid,
7580 +                                      task->p_pptr->comm, task->p_pptr->pid,
7581 +                                      task->p_pptr->uid, task->p_pptr->euid,
7582 +                                      task->p_pptr->gid, task->p_pptr->egid,
7583 +                                      task->uid,
7584 +                                      curr->res[GR_CRASH_RES].rlim_max / HZ);
7585 +                       write_lock(&gr_uid_lock);
7586 +                       gr_insert_uid(task->uid, curr->expires);
7587 +                       write_unlock(&gr_uid_lock);
7588 +                       curr->expires = 0;
7589 +                       curr->crashes = 0;
7590 +                       read_lock(&tasklist_lock);
7591 +                       for_each_task(tsk) {
7592 +                               if (tsk != task && tsk->uid == task->uid)
7593 +                                       gr_fake_force_sig(SIGKILL, tsk);
7594 +                       }
7595 +                       read_unlock(&tasklist_lock);
7596 +               } else {
7597 +                       security_alert(GR_SEGVNOSUID_ACL_MSG, task->comm,
7598 +                                      task->pid, task->uid, task->euid,
7599 +                                      task->gid, task->egid,
7600 +                                      task->p_pptr->comm, task->p_pptr->pid,
7601 +                                      task->p_pptr->uid, task->p_pptr->euid,
7602 +                                      task->p_pptr->gid, task->p_pptr->egid,
7603 +                                      kdevname(curr->device), curr->inode,
7604 +                                      curr->res[GR_CRASH_RES].rlim_max / HZ);
7605 +                       read_lock(&tasklist_lock);
7606 +                       for_each_task(tsk) {
7607 +                               if (likely(tsk != task)) {
7608 +                                       curr2 = tsk->acl;
7609 +
7610 +                                       if (curr2->device == curr->device &&
7611 +                                           curr2->inode == curr->inode)
7612 +                                               gr_fake_force_sig(SIGKILL, tsk);
7613 +                               }
7614 +                       }
7615 +                       read_unlock(&tasklist_lock);
7616 +               }
7617 +       }
7618 +
7619 +       return;
7620 +}
7621 +
7622 +int
7623 +gr_check_crash_exec(const struct file *filp)
7624 +{
7625 +       struct acl_subject_label *curr;
7626 +
7627 +       if (unlikely(!gr_acl_is_enabled()))
7628 +               return 0;
7629 +
7630 +       read_lock(&gr_inode_lock);
7631 +       curr = lookup_acl_subj_label(filp->f_dentry->d_inode->i_ino,
7632 +                                    filp->f_dentry->d_inode->i_dev,
7633 +                                    current->role);
7634 +       read_unlock(&gr_inode_lock);
7635 +
7636 +       if (!curr || !(curr->resmask & (1 << GR_CRASH_RES)) ||
7637 +           (!curr->crashes && !curr->expires))
7638 +               return 0;
7639 +
7640 +       if ((curr->crashes >= curr->res[GR_CRASH_RES].rlim_cur) &&
7641 +           time_after(curr->expires, jiffies))
7642 +               return 1;
7643 +       else if (time_before_eq(curr->expires, jiffies)) {
7644 +               curr->crashes = 0;
7645 +               curr->expires = 0;
7646 +       }
7647 +
7648 +       return 0;
7649 +}
7650 +
7651 +void
7652 +gr_handle_alertkill(void)
7653 +{
7654 +       struct acl_subject_label *curracl;
7655 +       __u32 curr_ip;
7656 +       struct task_struct *task;
7657 +
7658 +       if (unlikely(!gr_acl_is_enabled()))
7659 +               return;
7660 +
7661 +       curracl = current->acl;
7662 +       curr_ip = current->curr_ip;
7663 +
7664 +       if ((curracl->mode & GR_KILLIPPROC) && curr_ip &&
7665 +           (curr_ip != 0xffffffff)) {
7666 +               read_lock(&tasklist_lock);
7667 +               for_each_task(task) {
7668 +                       if (task->curr_ip == curr_ip)
7669 +                               gr_fake_force_sig(SIGKILL, task);
7670 +               }
7671 +               read_unlock(&tasklist_lock);
7672 +       } else if (curracl->mode & GR_KILLPROC)
7673 +               gr_fake_force_sig(SIGKILL, current);
7674 +
7675 +       return;
7676 +}
7677 diff -urN linux-2.4.21/grsecurity/gracl_shm.c linux-2.4.21/grsecurity/gracl_shm.c
7678 --- linux-2.4.21/grsecurity/gracl_shm.c 1969-12-31 19:00:00.000000000 -0500
7679 +++ linux-2.4.21/grsecurity/gracl_shm.c 2003-06-23 11:49:16.000000000 -0400
7680 @@ -0,0 +1,36 @@
7681 +/* shared memory handling routines, (c) Brad Spengler 2002, 2003 */
7682 +
7683 +#include <linux/kernel.h>
7684 +#include <linux/mm.h>
7685 +#include <linux/sched.h>
7686 +#include <linux/file.h>
7687 +#include <linux/ipc.h>
7688 +#include <linux/gracl.h>
7689 +#include <linux/grsecurity.h>
7690 +#include <linux/grinternal.h>
7691 +
7692 +int
7693 +gr_handle_shmat(const pid_t shm_cprid, const pid_t shm_lapid,
7694 +               const time_t shm_createtime, const uid_t cuid, const int shmid)
7695 +{
7696 +       struct task_struct *task;
7697 +
7698 +       if (!gr_acl_is_enabled())
7699 +               return 1;
7700 +
7701 +       task = find_task_by_pid(shm_cprid);
7702 +
7703 +       if (unlikely(!task))
7704 +               task = find_task_by_pid(shm_lapid);
7705 +
7706 +       if (unlikely(task && ((task->start_time < shm_createtime) ||
7707 +                             (task->pid == shm_lapid)) &&
7708 +                    (task->acl->mode & GR_PROTSHM) &&
7709 +                    (task->acl != current->acl))) {
7710 +               security_alert(GR_SHMAT_ACL_MSG, cuid, shm_cprid, shmid,
7711 +                              DEFAULTSECARGS);
7712 +               return 0;
7713 +       }
7714 +
7715 +       return 1;
7716 +}
7717 diff -urN linux-2.4.21/grsecurity/grsec_chdir.c linux-2.4.21/grsecurity/grsec_chdir.c
7718 --- linux-2.4.21/grsecurity/grsec_chdir.c       1969-12-31 19:00:00.000000000 -0500
7719 +++ linux-2.4.21/grsecurity/grsec_chdir.c       2003-06-23 11:49:16.000000000 -0400
7720 @@ -0,0 +1,20 @@
7721 +#include <linux/kernel.h>
7722 +#include <linux/sched.h>
7723 +#include <linux/fs.h>
7724 +#include <linux/file.h>
7725 +#include <linux/grsecurity.h>
7726 +#include <linux/grinternal.h>
7727 +
7728 +void
7729 +gr_log_chdir(const struct dentry *dentry, const struct vfsmount *mnt)
7730 +{
7731 +#ifdef CONFIG_GRKERNSEC_AUDIT_CHDIR
7732 +       if ((grsec_enable_chdir && grsec_enable_group &&
7733 +            in_group_p(grsec_audit_gid)) || (grsec_enable_chdir &&
7734 +                                             !grsec_enable_group)) {
7735 +               security_audit(GR_CHDIR_AUDIT_MSG, gr_to_filename(dentry, mnt),
7736 +                              DEFAULTSECARGS);
7737 +       }
7738 +#endif
7739 +       return;
7740 +}
7741 diff -urN linux-2.4.21/grsecurity/grsec_chroot.c linux-2.4.21/grsecurity/grsec_chroot.c
7742 --- linux-2.4.21/grsecurity/grsec_chroot.c      1969-12-31 19:00:00.000000000 -0500
7743 +++ linux-2.4.21/grsecurity/grsec_chroot.c      2003-06-23 11:49:16.000000000 -0400
7744 @@ -0,0 +1,322 @@
7745 +#include <linux/kernel.h>
7746 +#include <linux/sched.h>
7747 +#include <linux/file.h>
7748 +#include <linux/fs.h>
7749 +#include <linux/types.h>
7750 +#include <linux/grinternal.h>
7751 +
7752 +int
7753 +gr_handle_chroot_unix(const pid_t pid)
7754 +{
7755 +#ifdef CONFIG_GRKERNSEC_CHROOT_UNIX
7756 +       struct task_struct *p, **htable;
7757 +
7758 +       if (unlikely(!grsec_enable_chroot_unix))
7759 +               return 1;
7760 +
7761 +       if (likely(!proc_is_chrooted(current)))
7762 +               return 1;
7763 +
7764 +       read_lock(&tasklist_lock);
7765 +
7766 +       htable = &pidhash[pid_hashfn(pid)];
7767 +
7768 +       for (p = *htable; p && p->pid != pid; p = p->pidhash_next) ;
7769 +
7770 +       if (unlikely(p && !have_same_root(current, p))) {
7771 +               read_unlock(&tasklist_lock);
7772 +               security_alert(GR_UNIX_CHROOT_MSG, DEFAULTSECARGS);
7773 +               return 0;
7774 +       }
7775 +       read_unlock(&tasklist_lock);
7776 +#endif
7777 +       return 1;
7778 +}
7779 +
7780 +int
7781 +gr_handle_chroot_nice(void)
7782 +{
7783 +#ifdef CONFIG_GRKERNSEC_CHROOT_NICE
7784 +       if (grsec_enable_chroot_nice && proc_is_chrooted(current)) {
7785 +               security_alert(GR_NICE_CHROOT_MSG, DEFAULTSECARGS);
7786 +               return -EPERM;
7787 +       }
7788 +#endif
7789 +       return 0;
7790 +}
7791 +
7792 +int
7793 +gr_handle_chroot_setpriority(task_t *p, const int niceval)
7794 +{
7795 +#ifdef CONFIG_GRKERNSEC_CHROOT_NICE
7796 +       if (grsec_enable_chroot_nice && (!have_same_root(p, current)
7797 +                                        || (have_same_root(p, current)
7798 +                                            && (niceval < task_nice(p))
7799 +                                            && proc_is_chrooted(current)))) {
7800 +               security_alert(GR_PRIORITY_CHROOT_MSG, p->comm, p->pid,
7801 +                              DEFAULTSECARGS);
7802 +               return -ESRCH;
7803 +       }
7804 +#endif
7805 +       return 0;
7806 +}
7807 +
7808 +int
7809 +gr_handle_chroot_capset(const struct task_struct *target)
7810 +{
7811 +#ifdef CONFIG_GRKERNSEC_CHROOT_CAPS
7812 +       if (grsec_enable_chroot_caps && proc_is_chrooted(current) &&
7813 +           !have_same_root(current, target)) {
7814 +               security_alert(GR_CAPSET_CHROOT_MSG, target->comm, target->pid,
7815 +                              DEFAULTSECARGS);
7816 +               return 1;
7817 +       }
7818 +#endif
7819 +       return 0;
7820 +}
7821 +
7822 +int
7823 +gr_pid_is_chrooted(const struct task_struct *p)
7824 +{
7825 +#ifdef CONFIG_GRKERNSEC_CHROOT_FINDTASK
7826 +       if (!grsec_enable_chroot_findtask || (current->pid <= 1))
7827 +               return 0;
7828 +
7829 +       if (p && p->fs && p->fs->root && p->fs->root->d_inode &&
7830 +           child_reaper && child_reaper->fs && child_reaper->fs->root &&
7831 +           child_reaper->fs->root->d_inode && current && current->fs &&
7832 +           current->fs->root && current->fs->root->d_inode) {
7833 +               if (proc_is_chrooted(current) && !have_same_root(current, p))
7834 +                       return 1;
7835 +       }
7836 +#endif
7837 +       return 0;
7838 +}
7839 +
7840 +int
7841 +gr_chroot_fchdir(struct dentry *u_dentry, struct vfsmount *u_mnt)
7842 +{
7843 +#ifdef CONFIG_GRKERNSEC_CHROOT_FCHDIR
7844 +       if (!grsec_enable_chroot_fchdir)
7845 +               return 1;
7846 +
7847 +       if (!proc_is_chrooted(current))
7848 +               return 1;
7849 +       else {
7850 +               struct dentry *dentry = u_dentry;
7851 +               struct vfsmount *mnt = u_mnt;
7852 +               struct dentry *realroot;
7853 +               struct vfsmount *realrootmnt;
7854 +               struct dentry *currentroot;
7855 +               struct vfsmount *currentmnt;
7856 +
7857 +               read_lock(&child_reaper->fs->lock);
7858 +               realrootmnt = mntget(child_reaper->fs->rootmnt);
7859 +               realroot = dget(child_reaper->fs->root);
7860 +               read_unlock(&child_reaper->fs->lock);
7861 +
7862 +               read_lock(&current->fs->lock);
7863 +               currentmnt = mntget(current->fs->rootmnt);
7864 +               currentroot = dget(current->fs->root);
7865 +               read_unlock(&current->fs->lock);
7866 +
7867 +               spin_lock(&dcache_lock);
7868 +               for (;;) {
7869 +                       if (unlikely
7870 +                           ((dentry == realroot && mnt == realrootmnt)
7871 +                            || (dentry == currentroot && mnt == currentmnt)))
7872 +                               break;
7873 +                       if (unlikely
7874 +                           (dentry == mnt->mnt_root || IS_ROOT(dentry))) {
7875 +                               if (mnt->mnt_parent == mnt)
7876 +                                       break;
7877 +                               dentry = mnt->mnt_mountpoint;
7878 +                               mnt = mnt->mnt_parent;
7879 +                               continue;
7880 +                       }
7881 +                       dentry = dentry->d_parent;
7882 +               }
7883 +               spin_unlock(&dcache_lock);
7884 +
7885 +               dput(currentroot);
7886 +               mntput(currentmnt);
7887 +
7888 +               if (dentry == realroot && mnt == realrootmnt) {
7889 +                       /* ok, they're definitely trying to fchdir outside of the
7890 +                          chroot. */
7891 +                       dput(realroot);
7892 +                       mntput(realrootmnt);
7893 +                       security_alert(GR_CHROOT_FCHDIR_MSG,
7894 +                                      gr_to_filename(u_dentry, u_mnt),
7895 +                                      DEFAULTSECARGS);
7896 +                       return 0;
7897 +               } else {
7898 +                       dput(realroot);
7899 +                       mntput(realrootmnt);
7900 +                       return 1;
7901 +               }
7902 +       }
7903 +#endif
7904 +       return 1;
7905 +}
7906 +
7907 +int
7908 +gr_chroot_shmat(const pid_t shm_cprid, const pid_t shm_lapid,
7909 +               const time_t shm_createtime)
7910 +{
7911 +#ifdef CONFIG_GRKERNSEC_CHROOT_SHMAT
7912 +       struct task_struct *p, **htable;
7913 +
7914 +       if (unlikely(!grsec_enable_chroot_shmat))
7915 +               return 1;
7916 +
7917 +       if (likely(!proc_is_chrooted(current)))
7918 +               return 1;
7919 +
7920 +       read_lock(&tasklist_lock);
7921 +
7922 +       htable = &pidhash[pid_hashfn(shm_cprid)];
7923 +
7924 +       for (p = *htable; p && p->pid != shm_cprid; p = p->pidhash_next) ;
7925 +
7926 +       if (unlikely(p && !have_same_root(current, p) &&
7927 +                    (p->start_time < shm_createtime))) {
7928 +               read_unlock(&tasklist_lock);
7929 +               security_alert(GR_SHMAT_CHROOT_MSG, DEFAULTSECARGS);
7930 +               return 0;
7931 +       }
7932 +
7933 +       if (unlikely(!p)) {
7934 +               htable = &pidhash[pid_hashfn(shm_lapid)];
7935 +               for (p = *htable; p && p->pid != shm_lapid;
7936 +                    p = p->pidhash_next) ;
7937 +
7938 +               if (unlikely(p && !have_same_root(current, p))) {
7939 +                       read_unlock(&tasklist_lock);
7940 +                       security_alert(GR_SHMAT_CHROOT_MSG, DEFAULTSECARGS);
7941 +                       return 0;
7942 +               }
7943 +       }
7944 +
7945 +       read_unlock(&tasklist_lock);
7946 +#endif
7947 +       return 1;
7948 +}
7949 +
7950 +void
7951 +gr_log_chroot_exec(const struct dentry *dentry, const struct vfsmount *mnt)
7952 +{
7953 +#ifdef CONFIG_GRKERNSEC_CHROOT_EXECLOG
7954 +       if (grsec_enable_chroot_execlog && proc_is_chrooted(current))
7955 +               security_audit(GR_EXEC_CHROOT_MSG, gr_to_filename(dentry, mnt),
7956 +                              DEFAULTSECARGS);
7957 +#endif
7958 +       return;
7959 +}
7960 +
7961 +int
7962 +gr_handle_chroot_mknod(const struct dentry *dentry,
7963 +                      const struct vfsmount *mnt, const int mode)
7964 +{
7965 +#ifdef CONFIG_GRKERNSEC_CHROOT_MKNOD
7966 +       if (grsec_enable_chroot_mknod && !S_ISFIFO(mode) &&
7967 +           proc_is_chrooted(current)) {
7968 +               security_alert(GR_MKNOD_CHROOT_MSG,
7969 +                              gr_to_filename(dentry, mnt), DEFAULTSECARGS);
7970 +               return -EPERM;
7971 +       }
7972 +#endif
7973 +       return 0;
7974 +}
7975 +
7976 +int
7977 +gr_handle_chroot_mount(const struct dentry *dentry,
7978 +                      const struct vfsmount *mnt, const char *dev_name)
7979 +{
7980 +#ifdef CONFIG_GRKERNSEC_CHROOT_MOUNT
7981 +       if (grsec_enable_chroot_mount && proc_is_chrooted(current)) {
7982 +               security_alert(GR_MOUNT_CHROOT_MSG, dev_name,
7983 +                              gr_to_filename(dentry, mnt), DEFAULTSECARGS);
7984 +               return -EPERM;
7985 +       }
7986 +#endif
7987 +       return 0;
7988 +}
7989 +
7990 +int
7991 +gr_handle_chroot_pivot(void)
7992 +{
7993 +#ifdef CONFIG_GRKERNSEC_CHROOT_PIVOT
7994 +       if (grsec_enable_chroot_pivot && proc_is_chrooted(current)) {
7995 +               security_alert(GR_PIVOT_CHROOT_MSG, DEFAULTSECARGS);
7996 +               return -EPERM;
7997 +       }
7998 +#endif
7999 +       return 0;
8000 +}
8001 +
8002 +int
8003 +gr_handle_chroot_chroot(const struct dentry *dentry, const struct vfsmount *mnt)
8004 +{
8005 +#ifdef CONFIG_GRKERNSEC_CHROOT_DOUBLE
8006 +       if (grsec_enable_chroot_double && proc_is_chrooted(current)) {
8007 +               security_alert(GR_CHROOT_CHROOT_MSG,
8008 +                              gr_to_filename(dentry, mnt), DEFAULTSECARGS);
8009 +               return -EPERM;
8010 +       }
8011 +#endif
8012 +       return 0;
8013 +}
8014 +
8015 +void
8016 +gr_handle_chroot_caps(struct task_struct *task)
8017 +{
8018 +#ifdef CONFIG_GRKERNSEC_CHROOT_CAPS
8019 +       if (grsec_enable_chroot_caps && proc_is_chrooted(task)) {
8020 +               task->cap_permitted =
8021 +                   cap_drop(task->cap_permitted, GR_CHROOT_CAPS);
8022 +               task->cap_inheritable =
8023 +                   cap_drop(task->cap_inheritable, GR_CHROOT_CAPS);
8024 +               task->cap_effective =
8025 +                   cap_drop(task->cap_effective, GR_CHROOT_CAPS);
8026 +       }
8027 +#endif
8028 +       return;
8029 +}
8030 +
8031 +int
8032 +gr_handle_chroot_sysctl(const int op)
8033 +{
8034 +#ifdef CONFIG_GRKERNSEC_CHROOT_SYSCTL
8035 +       if (grsec_enable_chroot_sysctl && proc_is_chrooted(current)
8036 +           && (op & 002))
8037 +               return -EACCES;
8038 +#endif
8039 +       return 0;
8040 +}
8041 +
8042 +void
8043 +gr_handle_chroot_chdir(struct dentry *dentry, struct vfsmount *mnt)
8044 +{
8045 +#ifdef CONFIG_GRKERNSEC_CHROOT_CHDIR
8046 +       if (grsec_enable_chroot_chdir)
8047 +               set_fs_pwd(current->fs, mnt, dentry);
8048 +#endif
8049 +       return;
8050 +}
8051 +
8052 +int
8053 +gr_handle_chroot_chmod(const struct dentry *dentry,
8054 +                      const struct vfsmount *mnt, const int mode)
8055 +{
8056 +#ifdef CONFIG_GRKERNSEC_CHROOT_CHMOD
8057 +       if (grsec_enable_chroot_chmod &&
8058 +           ((mode & S_ISUID) || (mode & S_ISGID)) &&
8059 +           proc_is_chrooted(current)) {
8060 +               security_alert(GR_CHMOD_CHROOT_MSG,
8061 +                              gr_to_filename(dentry, mnt), DEFAULTSECARGS);
8062 +               return -EPERM;
8063 +       }
8064 +#endif
8065 +       return 0;
8066 +}
8067 diff -urN linux-2.4.21/grsecurity/grsec_disabled.c linux-2.4.21/grsecurity/grsec_disabled.c
8068 --- linux-2.4.21/grsecurity/grsec_disabled.c    1969-12-31 19:00:00.000000000 -0500
8069 +++ linux-2.4.21/grsecurity/grsec_disabled.c    2003-06-23 11:49:16.000000000 -0400
8070 @@ -0,0 +1,368 @@
8071 +/* 
8072 + * when grsecurity is disabled, compile all external functions into nothing
8073 + */
8074 +
8075 +#include <linux/kernel.h>
8076 +#include <linux/config.h>
8077 +#include <linux/sched.h>
8078 +#include <linux/file.h>
8079 +#include <linux/fs.h>
8080 +#include <linux/kdev_t.h>
8081 +#include <linux/net.h>
8082 +#include <linux/in.h>
8083 +#include <linux/ip.h>
8084 +#include <linux/skbuff.h>
8085 +#include <linux/sysctl.h>
8086 +
8087 +#ifdef CONFIG_SYSCTL
8088 +__u32
8089 +gr_handle_sysctl(const struct ctl_table * table, __u32 mode)
8090 +{
8091 +       return mode;
8092 +}
8093 +#endif
8094 +
8095 +int
8096 +gr_acl_is_enabled(void)
8097 +{
8098 +       return 0;
8099 +}
8100 +
8101 +void
8102 +gr_acl_handle_psacct(struct task_struct *task, const long code)
8103 +{
8104 +       return;
8105 +}
8106 +
8107 +int
8108 +gr_handle_ptrace_exec(const struct dentry *dentry, const struct vfsmount *mnt)
8109 +{
8110 +       return 0;
8111 +}
8112 +
8113 +int
8114 +gr_handle_mmap(const struct file *filp, const unsigned long prot)
8115 +{
8116 +       return 0;
8117 +}
8118 +
8119 +int
8120 +gr_handle_ptrace(struct task_struct *task, const long request)
8121 +{
8122 +       return 0;
8123 +}
8124 +
8125 +void
8126 +gr_learn_resource(const struct task_struct *task,
8127 +                 const int res, const unsigned long wanted)
8128 +{
8129 +       return;
8130 +}
8131 +
8132 +int
8133 +gr_set_acls(const int type)
8134 +{
8135 +       return 0;
8136 +}
8137 +
8138 +int
8139 +gr_check_hidden_task(const struct task_struct *tsk)
8140 +{
8141 +       return 0;
8142 +}
8143 +
8144 +int
8145 +gr_check_protected_task(const struct task_struct *task)
8146 +{
8147 +       return 0;
8148 +}
8149 +
8150 +__inline__ void
8151 +gr_copy_label(struct task_struct *tsk)
8152 +{
8153 +       return;
8154 +}
8155 +
8156 +void
8157 +gr_set_proc_label(const struct dentry *dentry, const struct vfsmount *mnt)
8158 +{
8159 +       return;
8160 +}
8161 +
8162 +void
8163 +gr_handle_delete(const ino_t ino, const kdev_t dev)
8164 +{
8165 +       return;
8166 +}
8167 +
8168 +void
8169 +gr_handle_create(const struct dentry *dentry, const struct vfsmount *mnt)
8170 +{
8171 +       return;
8172 +}
8173 +
8174 +void
8175 +gr_handle_crash(struct task_struct *task, const int sig)
8176 +{
8177 +       return;
8178 +}
8179 +
8180 +int
8181 +gr_check_crash_exec(const struct file *filp)
8182 +{
8183 +       return 0;
8184 +}
8185 +
8186 +int
8187 +gr_check_crash_uid(const uid_t uid)
8188 +{
8189 +       return 0;
8190 +}
8191 +
8192 +int
8193 +gr_handle_rename(struct inode *old_dir, struct inode *new_dir,
8194 +                struct dentry *old_dentry,
8195 +                struct dentry *new_dentry,
8196 +                struct vfsmount *mnt, const __u8 replace)
8197 +{
8198 +       return 0;
8199 +}
8200 +
8201 +int
8202 +gr_search_socket(const int family, const int type, const int protocol)
8203 +{
8204 +       return 1;
8205 +}
8206 +
8207 +int
8208 +gr_search_connectbind(const int mode, const struct socket *sock,
8209 +                     const struct sockaddr_in *addr)
8210 +{
8211 +       return 1;
8212 +}
8213 +
8214 +int
8215 +gr_is_capable(const int cap)
8216 +{
8217 +       return 1;
8218 +}
8219 +
8220 +void
8221 +gr_handle_alertkill(void)
8222 +{
8223 +       return;
8224 +}
8225 +
8226 +__u32
8227 +gr_acl_handle_execve(const struct dentry * dentry, const struct vfsmount * mnt)
8228 +{
8229 +       return 1;
8230 +}
8231 +
8232 +__u32
8233 +gr_acl_handle_hidden_file(const struct dentry * dentry,
8234 +                         const struct vfsmount * mnt)
8235 +{
8236 +       return 1;
8237 +}
8238 +
8239 +__u32
8240 +gr_acl_handle_open(const struct dentry * dentry, const struct vfsmount * mnt,
8241 +                  const int fmode)
8242 +{
8243 +       return 1;
8244 +}
8245 +
8246 +__u32
8247 +gr_acl_handle_rmdir(const struct dentry * dentry, const struct vfsmount * mnt)
8248 +{
8249 +       return 1;
8250 +}
8251 +
8252 +__u32
8253 +gr_acl_handle_unlink(const struct dentry * dentry, const struct vfsmount * mnt)
8254 +{
8255 +       return 1;
8256 +}
8257 +
8258 +int
8259 +gr_acl_handle_mmap(const struct file *file, const unsigned long prot,
8260 +                  unsigned int *vm_flags)
8261 +{
8262 +       return 1;
8263 +}
8264 +
8265 +__u32
8266 +gr_acl_handle_truncate(const struct dentry * dentry,
8267 +                      const struct vfsmount * mnt)
8268 +{
8269 +       return 1;
8270 +}
8271 +
8272 +__u32
8273 +gr_acl_handle_utime(const struct dentry * dentry, const struct vfsmount * mnt)
8274 +{
8275 +       return 1;
8276 +}
8277 +
8278 +__u32
8279 +gr_acl_handle_access(const struct dentry * dentry,
8280 +                    const struct vfsmount * mnt, const int fmode)
8281 +{
8282 +       return 1;
8283 +}
8284 +
8285 +__u32
8286 +gr_acl_handle_fchmod(const struct dentry * dentry, const struct vfsmount * mnt,
8287 +                    mode_t mode)
8288 +{
8289 +       return 1;
8290 +}
8291 +
8292 +__u32
8293 +gr_acl_handle_chmod(const struct dentry * dentry, const struct vfsmount * mnt,
8294 +                   mode_t mode)
8295 +{
8296 +       return 1;
8297 +}
8298 +
8299 +__u32
8300 +gr_acl_handle_chown(const struct dentry * dentry, const struct vfsmount * mnt)
8301 +{
8302 +       return 1;
8303 +}
8304 +
8305 +void
8306 +grsecurity_init(void)
8307 +{
8308 +       return;
8309 +}
8310 +
8311 +__u32
8312 +gr_acl_handle_mknod(const struct dentry * new_dentry,
8313 +                   const struct dentry * parent_dentry,
8314 +                   const struct vfsmount * parent_mnt,
8315 +                   const int mode)
8316 +{
8317 +       return 1;
8318 +}
8319 +
8320 +__u32
8321 +gr_acl_handle_mkdir(const struct dentry * new_dentry,
8322 +                   const struct dentry * parent_dentry,
8323 +                   const struct vfsmount * parent_mnt)
8324 +{
8325 +       return 1;
8326 +}
8327 +
8328 +__u32
8329 +gr_acl_handle_symlink(const struct dentry * new_dentry,
8330 +                     const struct dentry * parent_dentry,
8331 +                     const struct vfsmount * parent_mnt, const char *from)
8332 +{
8333 +       return 1;
8334 +}
8335 +
8336 +__u32
8337 +gr_acl_handle_link(const struct dentry * new_dentry,
8338 +                  const struct dentry * parent_dentry,
8339 +                  const struct vfsmount * parent_mnt,
8340 +                  const struct dentry * old_dentry,
8341 +                  const struct vfsmount * old_mnt, const char *to)
8342 +{
8343 +       return 1;
8344 +}
8345 +
8346 +int
8347 +gr_acl_handle_rename(const struct dentry *new_dentry,
8348 +                    const struct dentry *parent_dentry,
8349 +                    const struct vfsmount *parent_mnt,
8350 +                    const struct dentry *old_dentry,
8351 +                    const struct inode *old_parent_inode,
8352 +                    const struct vfsmount *old_mnt, const char *newname)
8353 +{
8354 +       return 1;
8355 +}
8356 +
8357 +__u32
8358 +gr_acl_handle_filldir(const struct dentry * dentry,
8359 +                     const struct vfsmount * mnt, const ino_t ino)
8360 +{
8361 +       return 1;
8362 +}
8363 +
8364 +int
8365 +gr_handle_shmat(const pid_t shm_cprid, const pid_t shm_lapid,
8366 +               const time_t shm_createtime, const uid_t cuid, const int shmid)
8367 +{
8368 +       return 1;
8369 +}
8370 +
8371 +int
8372 +gr_search_bind(const struct socket *sock, const struct sockaddr_in *addr)
8373 +{
8374 +       return 1;
8375 +}
8376 +
8377 +int
8378 +gr_search_connect(const struct socket *sock, const struct sockaddr_in *addr)
8379 +{
8380 +       return 1;
8381 +}
8382 +
8383 +__u32
8384 +gr_acl_handle_unix(const struct dentry * dentry, const struct vfsmount * mnt)
8385 +{
8386 +       return 1;
8387 +}
8388 +
8389 +__u32
8390 +gr_acl_handle_creat(const struct dentry * dentry,
8391 +                   const struct dentry * p_dentry,
8392 +                   const struct vfsmount * p_mnt, const int fmode,
8393 +                   const int imode)
8394 +{
8395 +       return 1;
8396 +}
8397 +
8398 +void
8399 +gr_acl_handle_exit(void)
8400 +{
8401 +       return;
8402 +}
8403 +
8404 +int
8405 +gr_acl_handle_mprotect(const struct file *file, const unsigned long prot)
8406 +{
8407 +       return 1;
8408 +}
8409 +
8410 +void
8411 +gr_set_role_label(const uid_t uid, const gid_t gid)
8412 +{
8413 +       return;
8414 +}
8415 +
8416 +int
8417 +gr_acl_handle_procpidmem(const struct task_struct *task)
8418 +{
8419 +       return 0;
8420 +}
8421 +
8422 +int
8423 +gr_search_udp_recvmsg(const struct sock *sk, const struct sk_buff *skb)
8424 +{
8425 +       return 1;
8426 +}
8427 +
8428 +int
8429 +gr_search_udp_sendmsg(const struct sock *sk, const struct sockaddr_in *addr)
8430 +{
8431 +       return 1;
8432 +}
8433 +
8434 +void
8435 +gr_set_kernel_label(struct task_struct *task)
8436 +{
8437 +       return;
8438 +}
8439 diff -urN linux-2.4.21/grsecurity/grsec_exec.c linux-2.4.21/grsecurity/grsec_exec.c
8440 --- linux-2.4.21/grsecurity/grsec_exec.c        1969-12-31 19:00:00.000000000 -0500
8441 +++ linux-2.4.21/grsecurity/grsec_exec.c        2003-06-23 11:49:16.000000000 -0400
8442 @@ -0,0 +1,70 @@
8443 +#include <linux/kernel.h>
8444 +#include <linux/sched.h>
8445 +#include <linux/file.h>
8446 +#include <linux/fs.h>
8447 +#include <linux/types.h>
8448 +#include <linux/grdefs.h>
8449 +#include <linux/grinternal.h>
8450 +#include <linux/capability.h>
8451 +
8452 +#include <asm/uaccess.h>
8453 +
8454 +int
8455 +gr_handle_nproc(void)
8456 +{
8457 +#ifdef CONFIG_GRKERNSEC_EXECVE
8458 +       if (grsec_enable_execve && current->user &&
8459 +           (atomic_read(&current->user->processes) >
8460 +            current->rlim[RLIMIT_NPROC].rlim_cur) &&
8461 +           !capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RESOURCE)) {
8462 +               security_alert(GR_NPROC_MSG, DEFAULTSECARGS);
8463 +               return -EAGAIN;
8464 +       }
8465 +#endif
8466 +       return 0;
8467 +}
8468 +
8469 +void
8470 +gr_handle_exec_args(struct linux_binprm *bprm, char **argv)
8471 +{
8472 +#ifdef CONFIG_GRKERNSEC_EXECLOG
8473 +       char grarg[64] = { 0 };
8474 +       __u8 execlen = 0;
8475 +       unsigned int i;
8476 +
8477 +       if (!((grsec_enable_execlog && grsec_enable_group &&
8478 +              in_group_p(grsec_audit_gid))
8479 +             || (grsec_enable_execlog && !grsec_enable_group)))
8480 +               return;
8481 +
8482 +       if (unlikely(!argv))
8483 +               goto log;
8484 +
8485 +       for (i = 0; i < bprm->argc && execlen < 62; i++) {
8486 +               char *p;
8487 +               __u8 len;
8488 +
8489 +               if (get_user(p, argv + i))
8490 +                       goto log;
8491 +               if (!p)
8492 +                       goto log;
8493 +               len = strnlen_user(p, 62 - execlen);
8494 +               if (len > 62 - execlen)
8495 +                       len = 62 - execlen;
8496 +               else if (len > 0)
8497 +                       len--;
8498 +               if (copy_from_user(grarg + execlen, p, len))
8499 +                       goto log;
8500 +               execlen += len;
8501 +               *(grarg + execlen) = ' ';
8502 +               *(grarg + execlen + 1) = '\0';
8503 +               execlen++;
8504 +       }
8505 +
8506 +      log:
8507 +       security_audit(GR_EXEC_AUDIT_MSG, gr_to_filename(bprm->file->f_dentry,
8508 +                                                        bprm->file->f_vfsmnt),
8509 +                      grarg, DEFAULTSECARGS);
8510 +#endif
8511 +       return;
8512 +}
8513 diff -urN linux-2.4.21/grsecurity/grsec_fifo.c linux-2.4.21/grsecurity/grsec_fifo.c
8514 --- linux-2.4.21/grsecurity/grsec_fifo.c        1969-12-31 19:00:00.000000000 -0500
8515 +++ linux-2.4.21/grsecurity/grsec_fifo.c        2003-06-23 11:49:16.000000000 -0400
8516 @@ -0,0 +1,24 @@
8517 +#include <linux/kernel.h>
8518 +#include <linux/sched.h>
8519 +#include <linux/fs.h>
8520 +#include <linux/file.h>
8521 +#include <linux/grinternal.h>
8522 +
8523 +int
8524 +gr_handle_fifo(const struct dentry *dentry, const struct vfsmount *mnt,
8525 +              const struct dentry *dir, const int flag, const int acc_mode)
8526 +{
8527 +#ifdef CONFIG_GRKERNSEC_FIFO
8528 +       if (grsec_enable_fifo && S_ISFIFO(dentry->d_inode->i_mode) &&
8529 +           !(flag & O_EXCL) && (dir->d_inode->i_mode & S_ISVTX) &&
8530 +           (dentry->d_inode->i_uid != dir->d_inode->i_uid) &&
8531 +           (current->fsuid != dentry->d_inode->i_uid)) {
8532 +               if (!permission(dentry->d_inode, acc_mode))
8533 +                       security_alert(GR_FIFO_MSG, gr_to_filename(dentry, mnt),
8534 +                                      dentry->d_inode->i_uid,
8535 +                                      dentry->d_inode->i_gid, DEFAULTSECARGS);
8536 +               return -EACCES;
8537 +       }
8538 +#endif
8539 +       return 0;
8540 +}
8541 diff -urN linux-2.4.21/grsecurity/grsec_fork.c linux-2.4.21/grsecurity/grsec_fork.c
8542 --- linux-2.4.21/grsecurity/grsec_fork.c        1969-12-31 19:00:00.000000000 -0500
8543 +++ linux-2.4.21/grsecurity/grsec_fork.c        2003-06-23 11:49:16.000000000 -0400
8544 @@ -0,0 +1,14 @@
8545 +#include <linux/kernel.h>
8546 +#include <linux/sched.h>
8547 +#include <linux/grsecurity.h>
8548 +#include <linux/grinternal.h>
8549 +
8550 +void
8551 +gr_log_forkfail(const int retval)
8552 +{
8553 +#ifdef CONFIG_GRKERNSEC_FORKFAIL
8554 +       if (grsec_enable_forkfail)
8555 +               security_alert(GR_FAILFORK_MSG, retval, DEFAULTSECARGS);
8556 +#endif
8557 +       return;
8558 +}
8559 diff -urN linux-2.4.21/grsecurity/grsec_init.c linux-2.4.21/grsecurity/grsec_init.c
8560 --- linux-2.4.21/grsecurity/grsec_init.c        1969-12-31 19:00:00.000000000 -0500
8561 +++ linux-2.4.21/grsecurity/grsec_init.c        2003-07-29 17:58:19.000000000 -0400
8562 @@ -0,0 +1,199 @@
8563 +#include <linux/kernel.h>
8564 +#include <linux/sched.h>
8565 +#include <linux/mm.h>
8566 +#include <linux/smp_lock.h>
8567 +
8568 +int grsec_enable_link;
8569 +int grsec_enable_dmesg;
8570 +int grsec_enable_fifo;
8571 +int grsec_enable_execve;
8572 +int grsec_enable_execlog;
8573 +int grsec_enable_signal;
8574 +int grsec_enable_forkfail;
8575 +int grsec_enable_time;
8576 +int grsec_enable_group;
8577 +int grsec_audit_gid;
8578 +int grsec_enable_chdir;
8579 +int grsec_enable_audit_ipc;
8580 +int grsec_enable_mount;
8581 +int grsec_enable_chroot_findtask;
8582 +int grsec_enable_chroot_mount;
8583 +int grsec_enable_chroot_shmat;
8584 +int grsec_enable_chroot_fchdir;
8585 +int grsec_enable_chroot_double;
8586 +int grsec_enable_chroot_pivot;
8587 +int grsec_enable_chroot_chdir;
8588 +int grsec_enable_chroot_chmod;
8589 +int grsec_enable_chroot_mknod;
8590 +int grsec_enable_chroot_nice;
8591 +int grsec_enable_chroot_execlog;
8592 +int grsec_enable_chroot_caps;
8593 +int grsec_enable_chroot_sysctl;
8594 +int grsec_enable_chroot_unix;
8595 +int grsec_enable_tpe;
8596 +int grsec_tpe_gid;
8597 +int grsec_enable_tpe_all;
8598 +int grsec_enable_randpid;
8599 +int grsec_enable_randid;
8600 +int grsec_enable_randisn;
8601 +int grsec_enable_randsrc;
8602 +int grsec_enable_randrpc;
8603 +int grsec_enable_socket_all;
8604 +int grsec_socket_all_gid;
8605 +int grsec_enable_socket_client;
8606 +int grsec_socket_client_gid;
8607 +int grsec_enable_socket_server;
8608 +int grsec_socket_server_gid;
8609 +int grsec_lock;
8610 +
8611 +spinlock_t grsec_alert_lock = SPIN_LOCK_UNLOCKED;
8612 +unsigned long grsec_alert_wtime = 0;
8613 +unsigned long grsec_alert_fyet = 0;
8614 +
8615 +spinlock_t grsec_alertgood_lock = SPIN_LOCK_UNLOCKED;
8616 +unsigned long grsec_alertgood_wtime = 0;
8617 +unsigned long grsec_alertgood_fyet = 0;
8618 +
8619 +spinlock_t grsec_audit_lock = SPIN_LOCK_UNLOCKED;
8620 +
8621 +char *gr_shared_page[2][NR_CPUS];
8622 +
8623 +extern void arc4_init(void);
8624 +
8625 +void
8626 +grsecurity_init(void)
8627 +{
8628 +       int i, j;
8629 +       /* create the per-cpu shared pages */
8630 +
8631 +       for (j = 0; j < 2; j++) {
8632 +               for (i = 0; i < NR_CPUS; i++) {
8633 +                       gr_shared_page[j][i] = (char *) get_zeroed_page(GFP_KERNEL);
8634 +                       if (!gr_shared_page[j][i]) {
8635 +                               panic("Unable to allocate grsecurity shared page");
8636 +                               return;
8637 +                       }
8638 +               }
8639 +       }
8640 +
8641 +       arc4_init();
8642 +
8643 +#ifndef CONFIG_GRKERNSEC_SYSCTL
8644 +       grsec_lock = 1;
8645 +#ifdef CONFIG_GRKERNSEC_AUDIT_GROUP
8646 +       grsec_enable_group = 1;
8647 +       grsec_audit_gid = CONFIG_GRKERNSEC_AUDIT_GID;
8648 +#endif
8649 +#ifdef CONFIG_GRKERNSEC_AUDIT_CHDIR
8650 +       grsec_enable_chdir = 1;
8651 +#endif
8652 +#ifdef CONFIG_GRKERNSEC_AUDIT_IPC
8653 +       grsec_enable_audit_ipc = 1;
8654 +#endif
8655 +#ifdef CONFIG_GRKERNSEC_AUDIT_MOUNT
8656 +       grsec_enable_mount = 1;
8657 +#endif
8658 +#ifdef CONFIG_GRKERNSEC_LINK
8659 +       grsec_enable_link = 1;
8660 +#endif
8661 +#ifdef CONFIG_GRKERNSEC_DMESG
8662 +       grsec_enable_dmesg = 1;
8663 +#endif
8664 +#ifdef CONFIG_GRKERNSEC_FIFO
8665 +       grsec_enable_fifo = 1;
8666 +#endif
8667 +#ifdef CONFIG_GRKERNSEC_EXECVE
8668 +       grsec_enable_execve = 1;
8669 +#endif
8670 +#ifdef CONFIG_GRKERNSEC_EXECLOG
8671 +       grsec_enable_execlog = 1;
8672 +#endif
8673 +#ifdef CONFIG_GRKERNSEC_SIGNAL
8674 +       grsec_enable_signal = 1;
8675 +#endif
8676 +#ifdef CONFIG_GRKERNSEC_FORKFAIL
8677 +       grsec_enable_forkfail = 1;
8678 +#endif
8679 +#ifdef CONFIG_GRKERNSEC_TIME
8680 +       grsec_enable_time = 1;
8681 +#endif
8682 +#ifdef CONFIG_GRKERNSEC_CHROOT_FINDTASK
8683 +       grsec_enable_chroot_findtask = 1;
8684 +#endif
8685 +#ifdef CONFIG_GRKERNSEC_CHROOT_UNIX
8686 +       grsec_enable_chroot_unix = 1;
8687 +#endif
8688 +#ifdef CONFIG_GRKERNSEC_CHROOT_MOUNT
8689 +       grsec_enable_chroot_mount = 1;
8690 +#endif
8691 +#ifdef CONFIG_GRKERNSEC_CHROOT_FCHDIR
8692 +       grsec_enable_chroot_fchdir = 1;
8693 +#endif
8694 +#ifdef CONFIG_GRKERNSEC_CHROOT_SHMAT
8695 +       grsec_enable_chroot_shmat = 1;
8696 +#endif
8697 +#ifdef CONFIG_GRKERNSEC_CHROOT_DOUBLE
8698 +       grsec_enable_chroot_double = 1;
8699 +#endif
8700 +#ifdef CONFIG_GRKERNSEC_CHROOT_PIVOT
8701 +       grsec_enable_chroot_pivot = 1;
8702 +#endif
8703 +#ifdef CONFIG_GRKERNSEC_CHROOT_CHDIR
8704 +       grsec_enable_chroot_chdir = 1;
8705 +#endif
8706 +#ifdef CONFIG_GRKERNSEC_CHROOT_CHMOD
8707 +       grsec_enable_chroot_chmod = 1;
8708 +#endif
8709 +#ifdef CONFIG_GRKERNSEC_CHROOT_MKNOD
8710 +       grsec_enable_chroot_mknod = 1;
8711 +#endif
8712 +#ifdef CONFIG_GRKERNSEC_CHROOT_NICE
8713 +       grsec_enable_chroot_nice = 1;
8714 +#endif
8715 +#ifdef CONFIG_GRKERNSEC_CHROOT_EXECLOG
8716 +       grsec_enable_chroot_execlog = 1;
8717 +#endif
8718 +#ifdef CONFIG_GRKERNSEC_CHROOT_CAPS
8719 +       grsec_enable_chroot_caps = 1;
8720 +#endif
8721 +#ifdef CONFIG_GRKERNSEC_CHROOT_SYSCTL
8722 +       grsec_enable_chroot_sysctl = 1;
8723 +#endif
8724 +#ifdef CONFIG_GRKERNSEC_TPE
8725 +       grsec_enable_tpe = 1;
8726 +       grsec_tpe_gid = CONFIG_GRKERNSEC_TPE_GID;
8727 +#ifdef CONFIG_GRKERNSEC_TPE_ALL
8728 +       grsec_enable_tpe_all = 1;
8729 +#endif
8730 +#endif
8731 +#ifdef CONFIG_GRKERNSEC_RANDPID
8732 +       grsec_enable_randpid = 1;
8733 +#endif
8734 +#ifdef CONFIG_GRKERNSEC_RANDID
8735 +       grsec_enable_randid = 1;
8736 +#endif
8737 +#ifdef CONFIG_GRKERNSEC_RANDISN
8738 +       grsec_enable_randisn = 1;
8739 +#endif
8740 +#ifdef CONFIG_GRKERNSEC_RANDSRC
8741 +       grsec_enable_randsrc = 1;
8742 +#endif
8743 +#ifdef CONFIG_GRKERNSEC_RANDRPC
8744 +       grsec_enable_randrpc = 1;
8745 +#endif
8746 +#ifdef CONFIG_GRKERNSEC_SOCKET_ALL
8747 +       grsec_enable_socket_all = 1;
8748 +       grsec_socket_all_gid = CONFIG_GRKERNSEC_SOCKET_ALL_GID;
8749 +#endif
8750 +#ifdef CONFIG_GRKERNSEC_SOCKET_CLIENT
8751 +       grsec_enable_socket_client = 1;
8752 +       grsec_socket_client_gid = CONFIG_GRKERNSEC_SOCKET_CLIENT_GID;
8753 +#endif
8754 +#ifdef CONFIG_GRKERNSEC_SOCKET_SERVER
8755 +       grsec_enable_socket_server = 1;
8756 +       grsec_socket_server_gid = CONFIG_GRKERNSEC_SOCKET_SERVER_GID;
8757 +#endif
8758 +#endif
8759 +
8760 +       return;
8761 +}
8762 diff -urN linux-2.4.21/grsecurity/grsec_ipc.c linux-2.4.21/grsecurity/grsec_ipc.c
8763 --- linux-2.4.21/grsecurity/grsec_ipc.c 1969-12-31 19:00:00.000000000 -0500
8764 +++ linux-2.4.21/grsecurity/grsec_ipc.c 2003-06-23 11:49:16.000000000 -0400
8765 @@ -0,0 +1,81 @@
8766 +#include <linux/kernel.h>
8767 +#include <linux/sched.h>
8768 +#include <linux/types.h>
8769 +#include <linux/ipc.h>
8770 +#include <linux/grsecurity.h>
8771 +#include <linux/grinternal.h>
8772 +
8773 +void
8774 +gr_log_msgget(const int ret, const int msgflg)
8775 +{
8776 +#ifdef CONFIG_GRKERNSEC_AUDIT_IPC
8777 +       if (((grsec_enable_group && in_group_p(grsec_audit_gid) &&
8778 +             grsec_enable_audit_ipc) || (grsec_enable_audit_ipc &&
8779 +                                         !grsec_enable_group)) && (ret >= 0)
8780 +           && (msgflg & IPC_CREAT))
8781 +               security_audit(GR_MSGQ_AUDIT_MSG, DEFAULTSECARGS);
8782 +#endif
8783 +       return;
8784 +}
8785 +
8786 +void
8787 +gr_log_msgrm(const uid_t uid, const uid_t cuid)
8788 +{
8789 +#ifdef CONFIG_GRKERNSEC_AUDIT_IPC
8790 +       if ((grsec_enable_group && in_group_p(grsec_audit_gid) &&
8791 +            grsec_enable_audit_ipc) ||
8792 +           (grsec_enable_audit_ipc && !grsec_enable_group))
8793 +               security_audit(GR_MSGQR_AUDIT_MSG, uid, cuid, DEFAULTSECARGS);
8794 +#endif
8795 +       return;
8796 +}
8797 +
8798 +void
8799 +gr_log_semget(const int err, const int semflg)
8800 +{
8801 +#ifdef CONFIG_GRKERNSEC_AUDIT_IPC
8802 +       if (((grsec_enable_group && in_group_p(grsec_audit_gid) &&
8803 +             grsec_enable_audit_ipc) || (grsec_enable_audit_ipc &&
8804 +                                         !grsec_enable_group)) && (err >= 0)
8805 +           && (semflg & IPC_CREAT))
8806 +               security_audit(GR_SEM_AUDIT_MSG, DEFAULTSECARGS);
8807 +#endif
8808 +       return;
8809 +}
8810 +
8811 +void
8812 +gr_log_semrm(const uid_t uid, const uid_t cuid)
8813 +{
8814 +#ifdef CONFIG_GRKERNSEC_AUDIT_IPC
8815 +       if ((grsec_enable_group && in_group_p(grsec_audit_gid) &&
8816 +            grsec_enable_audit_ipc) ||
8817 +           (grsec_enable_audit_ipc && !grsec_enable_group))
8818 +               security_audit(GR_SEMR_AUDIT_MSG, uid, cuid, DEFAULTSECARGS);
8819 +#endif
8820 +       return;
8821 +}
8822 +
8823 +void
8824 +gr_log_shmget(const int err, const int shmflg, const size_t size)
8825 +{
8826 +#ifdef CONFIG_GRKERNSEC_AUDIT_IPC
8827 +       if (((grsec_enable_group && in_group_p(grsec_audit_gid) &&
8828 +             grsec_enable_audit_ipc) || (grsec_enable_audit_ipc &&
8829 +                                         !grsec_enable_group)) && (err >= 0)
8830 +           && (shmflg & IPC_CREAT))
8831 +               security_audit(GR_SHM_AUDIT_MSG, size, DEFAULTSECARGS);
8832 +#endif
8833 +       return;
8834 +}
8835 +
8836 +void
8837 +gr_log_shmrm(const uid_t uid, const uid_t cuid)
8838 +{
8839 +#ifdef CONFIG_GRKERNSEC_AUDIT_IPC
8840 +       if ((grsec_enable_group && in_group_p(grsec_audit_gid) &&
8841 +            grsec_enable_audit_ipc) ||
8842 +           (grsec_enable_audit_ipc && !grsec_enable_group))
8843 +               security_audit(GR_SHMR_AUDIT_MSG, uid, cuid, DEFAULTSECARGS);
8844 +#endif
8845 +       return;
8846 +}
8847 diff -urN linux-2.4.21/grsecurity/grsec_link.c linux-2.4.21/grsecurity/grsec_link.c
8848 --- linux-2.4.21/grsecurity/grsec_link.c        1969-12-31 19:00:00.000000000 -0500
8849 +++ linux-2.4.21/grsecurity/grsec_link.c        2003-06-23 11:49:16.000000000 -0400
8850 @@ -0,0 +1,41 @@
8851 +#include <linux/kernel.h>
8852 +#include <linux/sched.h>
8853 +#include <linux/fs.h>
8854 +#include <linux/file.h>
8855 +#include <linux/grinternal.h>
8856 +
8857 +int
8858 +gr_handle_follow_link(const struct inode *parent,
8859 +                     const struct inode *inode,
8860 +                     const struct dentry *dentry, const struct vfsmount *mnt)
8861 +{
8862 +#ifdef CONFIG_GRKERNSEC_LINK
8863 +       if (grsec_enable_link && S_ISLNK(inode->i_mode) &&
8864 +           (parent->i_mode & S_ISVTX) && (parent->i_uid != inode->i_uid) &&
8865 +           (parent->i_mode & S_IWOTH) && (current->fsuid != inode->i_uid)) {
8866 +               security_alert(GR_SYMLINK_MSG, gr_to_filename(dentry, mnt),
8867 +                              inode->i_uid, inode->i_gid, DEFAULTSECARGS);
8868 +               return -EACCES;
8869 +       }
8870 +#endif
8871 +       return 0;
8872 +}
8873 +
8874 +int
8875 +gr_handle_hardlink(const struct dentry *dentry,
8876 +                  const struct vfsmount *mnt,
8877 +                  struct inode *inode, const int mode, const char *to)
8878 +{
8879 +#ifdef CONFIG_GRKERNSEC_LINK
8880 +       if (grsec_enable_link && current->fsuid != inode->i_uid &&
8881 +           (!S_ISREG(mode) || (mode & S_ISUID) ||
8882 +            ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) ||
8883 +            (permission(inode, MAY_READ | MAY_WRITE))) &&
8884 +           !capable(CAP_FOWNER) && current->uid) {
8885 +               security_alert(GR_HARDLINK_MSG, gr_to_filename(dentry, mnt),
8886 +                              inode->i_uid, inode->i_gid, to, DEFAULTSECARGS);
8887 +               return -EPERM;
8888 +       }
8889 +#endif
8890 +       return 0;
8891 +}
8892 diff -urN linux-2.4.21/grsecurity/grsec_mem.c linux-2.4.21/grsecurity/grsec_mem.c
8893 --- linux-2.4.21/grsecurity/grsec_mem.c 1969-12-31 19:00:00.000000000 -0500
8894 +++ linux-2.4.21/grsecurity/grsec_mem.c 2003-06-23 11:49:16.000000000 -0400
8895 @@ -0,0 +1,54 @@
8896 +#include <linux/kernel.h>
8897 +#include <linux/sched.h>
8898 +#include <linux/mm.h>
8899 +#include <linux/mman.h>
8900 +#include <linux/grinternal.h>
8901 +
8902 +void
8903 +gr_handle_ioperm(void)
8904 +{
8905 +       security_alert(GR_IOPERM_MSG, DEFAULTSECARGS);
8906 +       return;
8907 +}
8908 +
8909 +void
8910 +gr_handle_iopl(void)
8911 +{
8912 +       security_alert(GR_IOPL_MSG, DEFAULTSECARGS);
8913 +       return;
8914 +}
8915 +
8916 +void
8917 +gr_handle_mem_write(void)
8918 +{
8919 +       security_alert(GR_MEM_WRITE_MSG, DEFAULTSECARGS);
8920 +       return;
8921 +}
8922 +
8923 +void
8924 +gr_handle_kmem_write(void)
8925 +{
8926 +       security_alert(GR_KMEM_MSG, DEFAULTSECARGS);
8927 +       return;
8928 +}
8929 +
8930 +void
8931 +gr_handle_open_port(void)
8932 +{
8933 +       security_alert(GR_PORT_OPEN_MSG, DEFAULTSECARGS);
8934 +       return;
8935 +}
8936 +
8937 +int
8938 +gr_handle_mem_mmap(const unsigned long offset, struct vm_area_struct *vma)
8939 +{
8940 +       if (offset < __pa(high_memory) &&
8941 +           (pgprot_val(vma->vm_page_prot) & PROT_WRITE) &&
8942 +           (offset != 0xa0000 || ((vma->vm_end - vma->vm_start) > 0x20000))) {
8943 +               security_alert(GR_MEM_MMAP_MSG, DEFAULTSECARGS);
8944 +               return -EPERM;
8945 +       } else if (offset < __pa(high_memory))
8946 +               vma->vm_flags &= ~VM_MAYWRITE;
8947 +
8948 +       return 0;
8949 +}
8950 diff -urN linux-2.4.21/grsecurity/grsec_mount.c linux-2.4.21/grsecurity/grsec_mount.c
8951 --- linux-2.4.21/grsecurity/grsec_mount.c       1969-12-31 19:00:00.000000000 -0500
8952 +++ linux-2.4.21/grsecurity/grsec_mount.c       2003-06-23 11:49:16.000000000 -0400
8953 @@ -0,0 +1,34 @@
8954 +#include <linux/kernel.h>
8955 +#include <linux/sched.h>
8956 +#include <linux/grsecurity.h>
8957 +#include <linux/grinternal.h>
8958 +
8959 +void
8960 +gr_log_remount(const char *devname, const int retval)
8961 +{
8962 +#ifdef CONFIG_GRKERNSEC_AUDIT_MOUNT
8963 +       if (grsec_enable_mount && (retval >= 0))
8964 +               security_audit(GR_REMOUNT_AUDIT_MSG, devname, DEFAULTSECARGS);
8965 +#endif
8966 +       return;
8967 +}
8968 +
8969 +void
8970 +gr_log_unmount(const char *devname, const int retval)
8971 +{
8972 +#ifdef CONFIG_GRKERNSEC_AUDIT_MOUNT
8973 +       if (grsec_enable_mount && (retval >= 0))
8974 +               security_audit(GR_UNMOUNT_AUDIT_MSG, devname, DEFAULTSECARGS);
8975 +#endif
8976 +       return;
8977 +}
8978 +
8979 +void
8980 +gr_log_mount(const char *from, const char *to, const int retval)
8981 +{
8982 +#ifdef CONFIG_GRKERNSEC_AUDIT_MOUNT
8983 +       if (grsec_enable_mount && (retval >= 0))
8984 +               security_audit(GR_MOUNT_AUDIT_MSG, from, to, DEFAULTSECARGS);
8985 +#endif
8986 +       return;
8987 +}
8988 diff -urN linux-2.4.21/grsecurity/grsec_rand.c linux-2.4.21/grsecurity/grsec_rand.c
8989 --- linux-2.4.21/grsecurity/grsec_rand.c        1969-12-31 19:00:00.000000000 -0500
8990 +++ linux-2.4.21/grsecurity/grsec_rand.c        2003-06-23 11:49:16.000000000 -0400
8991 @@ -0,0 +1,36 @@
8992 +#include <linux/kernel.h>
8993 +#include <linux/sched.h>
8994 +#include <linux/smp_lock.h>
8995 +#include <linux/grsecurity.h>
8996 +#include <linux/grinternal.h>
8997 +
8998 +extern int last_pid;
8999 +
9000 +int
9001 +gr_random_pid(spinlock_t * pid_lock)
9002 +{
9003 +#ifdef CONFIG_GRKERNSEC_RANDPID
9004 +       struct task_struct *p;
9005 +       int pid;
9006 +
9007 +       if (grsec_enable_randpid && current->fs->root) {
9008 +               read_lock(&tasklist_lock);
9009 +               spin_lock(pid_lock);
9010 +
9011 +             repeater:
9012 +
9013 +               pid = 1 + (arc4random() % PID_MAX);
9014 +
9015 +               for_each_task(p) {
9016 +                       if (p->pid == pid || p->pgrp == pid ||
9017 +                           p->tgid == pid || p->session == pid)
9018 +                               goto repeater;
9019 +               }
9020 +               last_pid = pid;
9021 +               spin_unlock(pid_lock);
9022 +               read_unlock(&tasklist_lock);
9023 +               return pid;
9024 +       }
9025 +#endif
9026 +       return 0;
9027 +}
9028 diff -urN linux-2.4.21/grsecurity/grsec_sig.c linux-2.4.21/grsecurity/grsec_sig.c
9029 --- linux-2.4.21/grsecurity/grsec_sig.c 1969-12-31 19:00:00.000000000 -0500
9030 +++ linux-2.4.21/grsecurity/grsec_sig.c 2003-06-23 11:49:16.000000000 -0400
9031 @@ -0,0 +1,43 @@
9032 +#include <linux/kernel.h>
9033 +#include <linux/sched.h>
9034 +#include <linux/grinternal.h>
9035 +
9036 +void
9037 +gr_log_signal(const int sig, const struct task_struct *t)
9038 +{
9039 +#ifdef CONFIG_GRKERNSEC_SIGNAL
9040 +       if (grsec_enable_signal && ((sig == SIGSEGV) || (sig == SIGILL) ||
9041 +                                   (sig == SIGABRT) || (sig == SIGBUS))) {
9042 +               if (t->pid == current->pid) {
9043 +                       security_alert_good(GR_UNISIGLOG_MSG, sig,
9044 +                                           DEFAULTSECARGS);
9045 +               } else {
9046 +                       security_alert_good(GR_DUALSIGLOG_MSG, sig, t->comm,
9047 +                                           t->pid, t->uid, t->euid, t->gid,
9048 +                                           t->egid, t->p_pptr->comm,
9049 +                                           t->p_pptr->pid, t->p_pptr->uid,
9050 +                                           t->p_pptr->euid, t->p_pptr->gid,
9051 +                                           t->p_pptr->egid, DEFAULTSECARGS);
9052 +               }
9053 +       }
9054 +#endif
9055 +       return;
9056 +}
9057 +
9058 +int
9059 +gr_handle_signal(const struct task_struct *p, const int sig)
9060 +{
9061 +#ifdef CONFIG_GRKERNSEC
9062 +       if (current->pid > 1 && gr_check_protected_task(p)) {
9063 +               security_alert(GR_SIG_ACL_MSG, sig, p->comm, p->pid, p->uid,
9064 +                              p->euid, p->gid, p->egid, p->p_pptr->comm,
9065 +                              p->p_pptr->pid, p->p_pptr->uid,
9066 +                              p->p_pptr->euid, p->p_pptr->gid,
9067 +                              p->p_pptr->egid, DEFAULTSECARGS);
9068 +               return -EPERM;
9069 +       } else if (gr_pid_is_chrooted(p)) {
9070 +               return -EPERM;
9071 +       }
9072 +#endif
9073 +       return 0;
9074 +}
9075 diff -urN linux-2.4.21/grsecurity/grsec_sock.c linux-2.4.21/grsecurity/grsec_sock.c
9076 --- linux-2.4.21/grsecurity/grsec_sock.c        1969-12-31 19:00:00.000000000 -0500
9077 +++ linux-2.4.21/grsecurity/grsec_sock.c        2003-06-23 11:49:16.000000000 -0400
9078 @@ -0,0 +1,123 @@
9079 +#include <linux/kernel.h>
9080 +#include <linux/sched.h>
9081 +#include <linux/file.h>
9082 +#include <linux/net.h>
9083 +#include <net/sock.h>
9084 +#include <linux/grsecurity.h>
9085 +#include <linux/grinternal.h>
9086 +#include <linux/gracl.h>
9087 +
9088 +void
9089 +gr_attach_curr_ip(const struct sock *sk)
9090 +{
9091 +#ifdef CONFIG_GRKERNSEC
9092 +       struct task_struct *p;
9093 +       unsigned int i;
9094 +       struct inode *inode;
9095 +       struct file *filp;
9096 +       struct socket *connect_sock;
9097 +
9098 +       if (unlikely(sk->protocol != IPPROTO_TCP))
9099 +               return;
9100 +
9101 +       read_lock(&tasklist_lock);
9102 +       for_each_task(p) {
9103 +               if (!p->used_connect)
9104 +                       continue;
9105 +               task_lock(p);
9106 +               if (unlikely(!p->files)) {
9107 +                       task_unlock(p);
9108 +                       continue;
9109 +               }
9110 +               read_lock(&p->files->file_lock);
9111 +               for (i = 0; i < p->files->max_fds; i++) {
9112 +                       filp = fcheck_files(p->files, i);
9113 +                       if (likely(!filp))
9114 +                               continue;
9115 +                       inode = filp->f_dentry->d_inode;
9116 +                       if (likely(!inode || !inode->i_sock))
9117 +                               continue;
9118 +                       connect_sock = &inode->u.socket_i;
9119 +                       if (unlikely(!connect_sock ||
9120 +                                    connect_sock->sk->protocol != IPPROTO_TCP))
9121 +                               continue;
9122 +                       if (unlikely(sk->rcv_saddr == connect_sock->sk->daddr &&
9123 +                                    sk->daddr == connect_sock->sk->rcv_saddr &&
9124 +                                    ntohs(sk->sport) ==
9125 +                                    ntohs(connect_sock->sk->dport)
9126 +                                    && ntohs(sk->dport) ==
9127 +                                    ntohs(connect_sock->sk->sport))) {
9128 +                               current->curr_ip = p->curr_ip;
9129 +                               current->used_accept = 1;
9130 +                               read_unlock(&p->files->file_lock);
9131 +                               task_unlock(p);
9132 +                               read_unlock(&tasklist_lock);
9133 +                               return;
9134 +                       }
9135 +               }
9136 +               read_unlock(&p->files->file_lock);
9137 +               task_unlock(p);
9138 +       }
9139 +       read_unlock(&tasklist_lock);
9140 +
9141 +       current->curr_ip = sk->daddr;
9142 +       current->used_accept = 1;
9143 +#endif
9144 +       return;
9145 +}
9146 +
9147 +int
9148 +gr_handle_sock_all(const int family, const int type, const int protocol)
9149 +{
9150 +#ifdef CONFIG_GRKERNSEC_SOCKET_ALL
9151 +       if (grsec_enable_socket_all && in_group_p(grsec_socket_all_gid) &&
9152 +           (family != AF_UNIX) && (family != AF_LOCAL)) {
9153 +               security_alert(GR_SOCK_MSG, family, type, protocol,
9154 +                              DEFAULTSECARGS);
9155 +               return -EACCES;
9156 +       }
9157 +#endif
9158 +       return 0;
9159 +}
9160 +
9161 +int
9162 +gr_handle_sock_server(const struct sockaddr *sck)
9163 +{
9164 +#ifdef CONFIG_GRKERNSEC_SOCKET_SERVER
9165 +       if (grsec_enable_socket_server &&
9166 +           in_group_p(grsec_socket_server_gid) &&
9167 +           sck && (sck->sa_family != AF_UNIX) &&
9168 +           (sck->sa_family != AF_LOCAL)) {
9169 +               security_alert(GR_BIND_MSG, DEFAULTSECARGS);
9170 +               return -EACCES;
9171 +       }
9172 +#endif
9173 +       return 0;
9174 +}
9175 +
9176 +int
9177 +gr_handle_sock_client(const struct sockaddr *sck)
9178 +{
9179 +#ifdef CONFIG_GRKERNSEC_SOCKET_CLIENT
9180 +       if (grsec_enable_socket_client && in_group_p(grsec_socket_client_gid) &&
9181 +           sck && (sck->sa_family != AF_UNIX) &&
9182 +           (sck->sa_family != AF_LOCAL)) {
9183 +               security_alert(GR_CONNECT_MSG, DEFAULTSECARGS);
9184 +               return -EACCES;
9185 +       }
9186 +#endif
9187 +       return 0;
9188 +}
9189 +
9190 +__u32
9191 +gr_cap_rtnetlink(void)
9192 +{
9193 +#ifdef CONFIG_GRKERNSEC
9194 +       if (!gr_acl_is_enabled())
9195 +               return current->cap_effective;
9196 +       else
9197 +               return (current->cap_effective & ~(current->acl->cap_lower));
9198 +#else
9199 +       return current->cap_effective;
9200 +#endif
9201 +}
9202 diff -urN linux-2.4.21/grsecurity/grsec_sysctl.c linux-2.4.21/grsecurity/grsec_sysctl.c
9203 --- linux-2.4.21/grsecurity/grsec_sysctl.c      1969-12-31 19:00:00.000000000 -0500
9204 +++ linux-2.4.21/grsecurity/grsec_sysctl.c      2003-06-23 11:49:16.000000000 -0400
9205 @@ -0,0 +1,16 @@
9206 +#include <linux/kernel.h>
9207 +#include <linux/sched.h>
9208 +#include <linux/sysctl.h>
9209 +#include <linux/grinternal.h>
9210 +
9211 +int
9212 +gr_handle_sysctl_mod(const char *dirname, const char *name, const int op)
9213 +{
9214 +#ifdef CONFIG_GRKERNSEC_SYSCTL
9215 +       if (!strcmp(dirname, "grsecurity") && grsec_lock && (op & 002)) {
9216 +               security_alert(GR_SYSCTL_MSG, name, DEFAULTSECARGS);
9217 +               return -EACCES;
9218 +       }
9219 +#endif
9220 +       return 0;
9221 +}
9222 diff -urN linux-2.4.21/grsecurity/grsec_time.c linux-2.4.21/grsecurity/grsec_time.c
9223 --- linux-2.4.21/grsecurity/grsec_time.c        1969-12-31 19:00:00.000000000 -0500
9224 +++ linux-2.4.21/grsecurity/grsec_time.c        2003-06-23 11:49:16.000000000 -0400
9225 @@ -0,0 +1,13 @@
9226 +#include <linux/kernel.h>
9227 +#include <linux/sched.h>
9228 +#include <linux/grinternal.h>
9229 +
9230 +void
9231 +gr_log_timechange(void)
9232 +{
9233 +#ifdef CONFIG_GRKERNSEC_TIME
9234 +       if (grsec_enable_time)
9235 +               security_alert_good(GR_TIME_MSG, DEFAULTSECARGS);
9236 +#endif
9237 +       return;
9238 +}
9239 diff -urN linux-2.4.21/grsecurity/grsec_tpe.c linux-2.4.21/grsecurity/grsec_tpe.c
9240 --- linux-2.4.21/grsecurity/grsec_tpe.c 1969-12-31 19:00:00.000000000 -0500
9241 +++ linux-2.4.21/grsecurity/grsec_tpe.c 2003-06-23 11:49:16.000000000 -0400
9242 @@ -0,0 +1,33 @@
9243 +#include <linux/kernel.h>
9244 +#include <linux/sched.h>
9245 +#include <linux/file.h>
9246 +#include <linux/fs.h>
9247 +#include <linux/grinternal.h>
9248 +
9249 +int
9250 +gr_tpe_allow(const struct file *file)
9251 +{
9252 +#ifdef CONFIG_GRKERNSEC_TPE
9253 +       struct inode *inode = file->f_dentry->d_parent->d_inode;
9254 +
9255 +       if (current->uid && grsec_enable_tpe && in_group_p(grsec_tpe_gid) &&
9256 +           (inode->i_uid || (!inode->i_uid && ((inode->i_mode & S_IWGRP) ||
9257 +                                               (inode->i_mode & S_IWOTH))))) {
9258 +               security_alert(GR_EXEC_TPE_MSG,
9259 +                              gr_to_filename(file->f_dentry, file->f_vfsmnt),
9260 +                              DEFAULTSECARGS);
9261 +               return 0;
9262 +       }
9263 +#ifdef CONFIG_GRKERNSEC_TPE_ALL
9264 +       if (current->uid && grsec_enable_tpe && grsec_enable_tpe_all &&
9265 +           ((inode->i_uid && (inode->i_uid != current->uid)) ||
9266 +            (inode->i_mode & S_IWGRP) || (inode->i_mode & S_IWOTH))) {
9267 +               security_alert(GR_EXEC_TPE_MSG,
9268 +                              gr_to_filename(file->f_dentry, file->f_vfsmnt),
9269 +                              DEFAULTSECARGS);
9270 +               return 0;
9271 +       }
9272 +#endif
9273 +#endif
9274 +       return 1;
9275 +}
9276 diff -urN linux-2.4.21/grsecurity/grsum.c linux-2.4.21/grsecurity/grsum.c
9277 --- linux-2.4.21/grsecurity/grsum.c     1969-12-31 19:00:00.000000000 -0500
9278 +++ linux-2.4.21/grsecurity/grsum.c     2003-06-23 11:49:16.000000000 -0400
9279 @@ -0,0 +1,297 @@
9280 +#include <linux/kernel.h>
9281 +#include <linux/sched.h>
9282 +#include <linux/gracl.h>
9283 +
9284 +/* digest-sha256.c,v 1.13 2002/10/02 22:02:08 hvr Exp $
9285 + *
9286 + * SHA-256 code by Jean-Luc Cooke <jlcooke@certainkey.com>.
9287 + * 
9288 + * Glue code originally by Andrew McDonald and Alan Smithee, mailed
9289 + * to maintainer on pulped trees.
9290 + *
9291 + * This program is free software; you can redistribute it and/or modify it
9292 + * under the terms of the GNU General Public License as published by the
9293 + * Free Software Foundation; either version 2, or (at your option) any
9294 + * later version.
9295 + *
9296 + */
9297 +
9298 +typedef struct {
9299 +       __u32 state[8];
9300 +       __u32 count[2];
9301 +       __u8 buf[128];
9302 +} sha256_ctx_t;
9303 +
9304 +static __inline__ __u32
9305 +generic_rotr32(const __u32 x, const unsigned bits)
9306 +{
9307 +       const unsigned n = bits % 32;
9308 +       return (x >> n) | (x << (32 - n));
9309 +}
9310 +
9311 +#define Ch(x,y,z)   ((x & y) ^ (~x & z))
9312 +#define Maj(x,y,z)  ((x & y) ^ ( x & z) ^ (y & z))
9313 +#define RORu32(x,y) generic_rotr32(x, y)
9314 +#define e0(x)       (RORu32(x, 2) ^ RORu32(x,13) ^ RORu32(x,22))
9315 +#define e1(x)       (RORu32(x, 6) ^ RORu32(x,11) ^ RORu32(x,25))
9316 +#define s0(x)       (RORu32(x, 7) ^ RORu32(x,18) ^ (x >> 3))
9317 +#define s1(x)       (RORu32(x,17) ^ RORu32(x,19) ^ (x >> 10))
9318 +
9319 +#define H0         0x6a09e667
9320 +#define H1         0xbb67ae85
9321 +#define H2         0x3c6ef372
9322 +#define H3         0xa54ff53a
9323 +#define H4         0x510e527f
9324 +#define H5         0x9b05688c
9325 +#define H6         0x1f83d9ab
9326 +#define H7         0x5be0cd19
9327 +
9328 +const static __u32 sha256_K[64] = {
9329 +       0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
9330 +       0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
9331 +       0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
9332 +       0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
9333 +       0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
9334 +       0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
9335 +       0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
9336 +       0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
9337 +       0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
9338 +       0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
9339 +       0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
9340 +       0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
9341 +       0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
9342 +       0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
9343 +       0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
9344 +       0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
9345 +};
9346 +
9347 +#define LOAD_OP(I)\
9348 + {\
9349 +  t1  = input[(4*I)  ] & 0xff; t1<<=8;\
9350 +  t1 |= input[(4*I)+1] & 0xff; t1<<=8;\
9351 +  t1 |= input[(4*I)+2] & 0xff; t1<<=8;\
9352 +  t1 |= input[(4*I)+3] & 0xff;\
9353 +  W[I] = t1;\
9354 + }
9355 +
9356 +#define BLEND_OP(I) W[I] = s1(W[I-2]) + W[I-7] + s0(W[I-15]) + W[I-16];
9357 +
9358 +static void
9359 +SHA256Transform(__u32 * state, const __u8 * input)
9360 +{
9361 +       __u32 a, b, c, d, e, f, g, h, t1, t2;
9362 +       __u32 W[64];
9363 +
9364 +       int i;
9365 +
9366 +       /* load the input */
9367 +       LOAD_OP(0);
9368 +       LOAD_OP(1);
9369 +       LOAD_OP(2);
9370 +       LOAD_OP(3);
9371 +       LOAD_OP(4);
9372 +       LOAD_OP(5);
9373 +       LOAD_OP(6);
9374 +       LOAD_OP(7);
9375 +       LOAD_OP(8);
9376 +       LOAD_OP(9);
9377 +       LOAD_OP(10);
9378 +       LOAD_OP(11);
9379 +       LOAD_OP(12);
9380 +       LOAD_OP(13);
9381 +       LOAD_OP(14);
9382 +       LOAD_OP(15);
9383 +
9384 +       /* now blend */
9385 +       for (i = 16; i < 64; i += 8) {
9386 +               BLEND_OP(i);
9387 +               BLEND_OP(i + 1);
9388 +               BLEND_OP(i + 2);
9389 +               BLEND_OP(i + 3);
9390 +               BLEND_OP(i + 4);
9391 +               BLEND_OP(i + 5);
9392 +               BLEND_OP(i + 6);
9393 +               BLEND_OP(i + 7);
9394 +       }
9395 +
9396 +       /* load the state into our registers */
9397 +       a = state[0];
9398 +       b = state[1];
9399 +       c = state[2];
9400 +       d = state[3];
9401 +       e = state[4];
9402 +       f = state[5];
9403 +       g = state[6];
9404 +       h = state[7];
9405 +
9406 +       /* now iterate */
9407 +       for (i = 0; i < 64; i += 8) {
9408 +               t1 = h + e1(e) + Ch(e, f, g) + sha256_K[i] + W[i];
9409 +               t2 = e0(a) + Maj(a, b, c);
9410 +               d += t1;
9411 +               h = t1 + t2;
9412 +               t1 = g + e1(d) + Ch(d, e, f) + sha256_K[i + 1] + W[i + 1];
9413 +               t2 = e0(h) + Maj(h, a, b);
9414 +               c += t1;
9415 +               g = t1 + t2;
9416 +               t1 = f + e1(c) + Ch(c, d, e) + sha256_K[i + 2] + W[i + 2];
9417 +               t2 = e0(g) + Maj(g, h, a);
9418 +               b += t1;
9419 +               f = t1 + t2;
9420 +               t1 = e + e1(b) + Ch(b, c, d) + sha256_K[i + 3] + W[i + 3];
9421 +               t2 = e0(f) + Maj(f, g, h);
9422 +               a += t1;
9423 +               e = t1 + t2;
9424 +               t1 = d + e1(a) + Ch(a, b, c) + sha256_K[i + 4] + W[i + 4];
9425 +               t2 = e0(e) + Maj(e, f, g);
9426 +               h += t1;
9427 +               d = t1 + t2;
9428 +               t1 = c + e1(h) + Ch(h, a, b) + sha256_K[i + 5] + W[i + 5];
9429 +               t2 = e0(d) + Maj(d, e, f);
9430 +               g += t1;
9431 +               c = t1 + t2;
9432 +               t1 = b + e1(g) + Ch(g, h, a) + sha256_K[i + 6] + W[i + 6];
9433 +               t2 = e0(c) + Maj(c, d, e);
9434 +               f += t1;
9435 +               b = t1 + t2;
9436 +               t1 = a + e1(f) + Ch(f, g, h) + sha256_K[i + 7] + W[i + 7];
9437 +               t2 = e0(b) + Maj(b, c, d);
9438 +               e += t1;
9439 +               a = t1 + t2;
9440 +       }
9441 +
9442 +       state[0] += a;
9443 +       state[1] += b;
9444 +       state[2] += c;
9445 +       state[3] += d;
9446 +       state[4] += e;
9447 +       state[5] += f;
9448 +       state[6] += g;
9449 +       state[7] += h;
9450 +
9451 +       /* clear any sensitive info... */
9452 +       a = b = c = d = e = f = g = h = t1 = t2 = 0;
9453 +       memset(W, 0, 64 * sizeof (__u32));
9454 +}
9455 +
9456 +static void
9457 +SHA256Init(sha256_ctx_t * C)
9458 +{
9459 +       C->state[0] = H0;
9460 +       C->state[1] = H1;
9461 +       C->state[2] = H2;
9462 +       C->state[3] = H3;
9463 +       C->state[4] = H4;
9464 +       C->state[5] = H5;
9465 +       C->state[6] = H6;
9466 +       C->state[7] = H7;
9467 +       C->count[0] = C->count[1] = 0;
9468 +       memset(C->buf, 0, 128);
9469 +}
9470 +
9471 +static void
9472 +SHA256Update(sha256_ctx_t * C, const __u8 * input, __u32 inputLen)
9473 +{
9474 +       __u32 i, index, partLen;
9475 +
9476 +       /* Compute number of bytes mod 128 */
9477 +       index = (__u32) ((C->count[0] >> 3) & 0x3f);
9478 +
9479 +       /* Update number of bits */
9480 +       if ((C->count[0] += (inputLen << 3)) < (inputLen << 3)) {
9481 +               C->count[1]++;
9482 +               C->count[1] += (inputLen >> 29);
9483 +       }
9484 +
9485 +       partLen = 64 - index;
9486 +
9487 +       /* Transform as many times as possible. */
9488 +       if (inputLen >= partLen) {
9489 +               memcpy(&C->buf[index], input, partLen);
9490 +               SHA256Transform(C->state, C->buf);
9491 +
9492 +               for (i = partLen; i + 63 < inputLen; i += 64)
9493 +                       SHA256Transform(C->state, &input[i]);
9494 +               index = 0;
9495 +       } else {
9496 +               i = 0;
9497 +       }
9498 +
9499 +       /* Buffer remaining input */
9500 +       memcpy(&C->buf[index], &input[i], inputLen - i);
9501 +}
9502 +
9503 +static void
9504 +SHA256Final(sha256_ctx_t * C, __u8 * digest)
9505 +{
9506 +       const static __u8 padding[64] = { 0x80, };
9507 +       __u8 bits[8];
9508 +       __u32 t, index, padLen;
9509 +       int i, j;
9510 +
9511 +       /* Save number of bits */
9512 +       t = C->count[0];
9513 +       bits[7] = t;
9514 +       t >>= 8;
9515 +       bits[6] = t;
9516 +       t >>= 8;
9517 +       bits[5] = t;
9518 +       t >>= 8;
9519 +       bits[4] = t;
9520 +       t = C->count[1];
9521 +       bits[3] = t;
9522 +       t >>= 8;
9523 +       bits[2] = t;
9524 +       t >>= 8;
9525 +       bits[1] = t;
9526 +       t >>= 8;
9527 +       bits[0] = t;
9528 +
9529 +       /* Pad out to 56 mod 64. */
9530 +       index = (C->count[0] >> 3) & 0x3f;
9531 +       padLen = (index < 56) ? (56 - index) : ((64 + 56) - index);
9532 +       SHA256Update(C, padding, padLen);
9533 +
9534 +       /* Append length (before padding) */
9535 +       SHA256Update(C, bits, sizeof (bits));
9536 +
9537 +       /* Store state in digest */
9538 +       for (i = j = 0; i < 8; i++, j += 4) {
9539 +               t = C->state[i];
9540 +               digest[j + 3] = t;
9541 +               t >>= 8;
9542 +               digest[j + 2] = t;
9543 +               t >>= 8;
9544 +               digest[j + 1] = t;
9545 +               t >>= 8;
9546 +               digest[j] = t;
9547 +       }
9548 +
9549 +       /* Zeroize sensitive information. */
9550 +       memset(C, 0, sizeof (sha256_ctx_t));
9551 +}
9552 +
9553 +int
9554 +chkpw(struct gr_arg *entry, unsigned char *salt, unsigned char *sum)
9555 +{
9556 +       sha256_ctx_t context;
9557 +       unsigned char temp_sum[GR_SHA_LEN];
9558 +       volatile int retval = 0;
9559 +       volatile int dummy = 0;
9560 +       unsigned int i;
9561 +
9562 +       SHA256Init(&context);
9563 +       SHA256Update(&context, salt, GR_SALT_LEN);
9564 +       SHA256Update(&context, entry->pw, strlen(entry->pw));
9565 +       SHA256Final(&context, temp_sum);
9566 +
9567 +       memset(entry->pw, 0, GR_PW_LEN);
9568 +
9569 +       for (i = 0; i < GR_SHA_LEN; i++)
9570 +               if (sum[i] != temp_sum[i])
9571 +                       retval = 1;
9572 +               else
9573 +                       dummy = 1;      // waste a cycle
9574 +
9575 +       return retval;
9576 +}
9577 diff -urN linux-2.4.21/grsecurity/obsd_rand.c linux-2.4.21/grsecurity/obsd_rand.c
9578 --- linux-2.4.21/grsecurity/obsd_rand.c 1969-12-31 19:00:00.000000000 -0500
9579 +++ linux-2.4.21/grsecurity/obsd_rand.c 2003-06-23 11:49:16.000000000 -0400
9580 @@ -0,0 +1,299 @@
9581 +
9582 +/*
9583 + * Copyright (c) 1996, 1997, 2000-2002 Michael Shalayeff.
9584 + * 
9585 + * Version 1.89, last modified 19-Sep-99
9586 + *    
9587 + * Copyright Theodore Ts'o, 1994, 1995, 1996, 1997, 1998, 1999.
9588 + * All rights reserved.
9589 + *
9590 + * Copyright 1998 Niels Provos <provos@citi.umich.edu>
9591 + * All rights reserved.
9592 + * Theo de Raadt <deraadt@openbsd.org> came up with the idea of using
9593 + * such a mathematical system to generate more random (yet non-repeating)
9594 + * ids to solve the resolver/named problem.  But Niels designed the
9595 + * actual system based on the constraints.
9596 + *
9597 + * Redistribution and use in source and binary forms, with or without
9598 + * modification, are permitted provided that the following conditions
9599 + * are met:
9600 + * 1. Redistributions of source code must retain the above copyright
9601 + *    notice, this list of conditions and the following disclaimer,
9602 + * 2. Redistributions in binary form must reproduce the above copyright
9603 + *    notice, this list of conditions and the following disclaimer in the
9604 + *    documentation and/or other materials provided with the distribution.
9605 + * 3. All advertising materials mentioning features or use of this software
9606 + *    must display the following acknowledgement:
9607 + *    This product includes software developed by Niels Provos.
9608 + * 4. The name of the author may not be used to endorse or promote products
9609 + *    derived from this software without specific prior written permission.
9610 + *
9611 + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
9612 + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
9613 + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
9614 + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
9615 + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
9616 + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
9617 + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
9618 + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
9619 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
9620 + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
9621 + */
9622 +
9623 +#include <linux/kernel.h>
9624 +#include <linux/sched.h>
9625 +#include <linux/timer.h>
9626 +#include <linux/smp_lock.h>
9627 +#include <linux/random.h>
9628 +
9629 +#define RU_OUT 180
9630 +#define RU_MAX 30000
9631 +#define RU_GEN 2
9632 +#define RU_N 32749
9633 +#define RU_AGEN 7
9634 +#define RU_M 31104
9635 +#define PFAC_N 3
9636 +const static __u16 pfacts[PFAC_N] = { 2, 3, 2729 };
9637 +
9638 +static __u16 ru_x;
9639 +static __u16 ru_seed, ru_seed2;
9640 +static __u16 ru_a, ru_b;
9641 +static __u16 ru_g;
9642 +static __u16 ru_counter = 0;
9643 +static __u16 ru_msb = 0;
9644 +static unsigned long ru_reseed = 0;
9645 +static __u32 tmp;
9646 +
9647 +static struct arc4_stream {
9648 +       __u8 s[256];
9649 +       unsigned int cnt;
9650 +       __u8 i;
9651 +       __u8 j;
9652 +} arc4random_state[NR_CPUS];
9653 +
9654 +#define TCP_RNDISS_ROUNDS      15
9655 +#define TCP_RNDISS_OUT         7200
9656 +#define TCP_RNDISS_MAX         30000
9657 +
9658 +static __u8 tcp_rndiss_sbox[NR_CPUS][128];
9659 +static __u16 tcp_rndiss_msb[NR_CPUS];
9660 +static __u16 tcp_rndiss_cnt[NR_CPUS];
9661 +static unsigned long tcp_rndiss_reseed[NR_CPUS];
9662 +
9663 +static unsigned long arc4random_reseed[NR_CPUS];
9664 +
9665 +static int arc4random_initialized[NR_CPUS];
9666 +
9667 +static __u16 pmod(__u16, __u16, __u16);
9668 +static void ip_initid(void);
9669 +__u16 ip_randomid(void);
9670 +
9671 +static __inline__ __u8 arc4_getbyte(void);
9672 +static __inline__ void arc4_stir(void);
9673 +static void arc4maybeinit(void);
9674 +__u32 arc4random(void);
9675 +
9676 +static __inline__ __u8
9677 +arc4_getbyte(void)
9678 +{
9679 +       register __u8 si, sj;
9680 +       int cpu = smp_processor_id();
9681 +
9682 +       arc4random_state[cpu].cnt++;
9683 +       arc4random_state[cpu].i++;
9684 +       si = arc4random_state[cpu].s[arc4random_state[cpu].i];
9685 +       arc4random_state[cpu].j += si;
9686 +       sj = arc4random_state[cpu].s[arc4random_state[cpu].j];
9687 +       arc4random_state[cpu].s[arc4random_state[cpu].i] = sj;
9688 +       arc4random_state[cpu].s[arc4random_state[cpu].j] = si;
9689 +       return arc4random_state[cpu].s[(si + sj) & 0xff];
9690 +}
9691 +
9692 +static __inline__ void
9693 +arc4_stir(void)
9694 +{
9695 +       __u8 buf[256];
9696 +       register __u8 si;
9697 +       register int n;
9698 +       int len = sizeof (buf);
9699 +       int cpu = smp_processor_id();
9700 +       unsigned long flags;
9701 +
9702 +       do_gettimeofday((struct timeval *) buf);
9703 +       get_random_bytes(buf + sizeof (struct timeval),
9704 +                        len - sizeof (struct timeval));
9705 +
9706 +       save_flags(flags);
9707 +       cli();
9708 +
9709 +       arc4random_state[cpu].i--;
9710 +       for (n = 0; n < 256; n++) {
9711 +               arc4random_state[cpu].i++;
9712 +               si = arc4random_state[cpu].s[arc4random_state[cpu].i];
9713 +               arc4random_state[cpu].j += si + buf[n % len];
9714 +               arc4random_state[cpu].s[arc4random_state[cpu].i] =
9715 +                   arc4random_state[cpu].s[arc4random_state[cpu].j];
9716 +               arc4random_state[cpu].s[arc4random_state[cpu].j] = si;
9717 +       }
9718 +       arc4random_state[cpu].j = arc4random_state[cpu].i;
9719 +       arc4random_state[cpu].cnt = 0;
9720 +
9721 +       restore_flags(flags);
9722 +
9723 +       for (n = 0; n < 256 * 4; n++)
9724 +               arc4_getbyte();
9725 +
9726 +       return;
9727 +}
9728 +
9729 +void
9730 +arc4_init(void)
9731 +{
9732 +       int i, n;
9733 +
9734 +       for (n = 0; n < NR_CPUS; n++) {
9735 +               for (i = 0; i < 256; i++)
9736 +                       arc4random_state[n].s[i] = i;
9737 +               arc4random_initialized[n] = 0;
9738 +       }
9739 +}
9740 +
9741 +static void
9742 +arc4maybeinit(void)
9743 +{
9744 +       int cpu = smp_processor_id();
9745 +
9746 +       if (arc4random_initialized[cpu] &&
9747 +           time_after(xtime.tv_sec, arc4random_reseed[cpu]))
9748 +               arc4random_initialized[cpu] = 0;
9749 +
9750 +       if (!arc4random_initialized[cpu]) {
9751 +               arc4random_initialized[cpu]++;
9752 +               arc4_stir();
9753 +               arc4random_reseed[cpu] = xtime.tv_sec + (10 * 60);
9754 +       }
9755 +}
9756 +
9757 +__u32
9758 +arc4random(void)
9759 +{
9760 +       arc4maybeinit();
9761 +       return ((arc4_getbyte() << 24) | (arc4_getbyte() << 16)
9762 +               | (arc4_getbyte() << 8) | arc4_getbyte());
9763 +}
9764 +
9765 +static __u16
9766 +pmod(__u16 gen, __u16 exp, __u16 mod)
9767 +{
9768 +       __u16 s, t, u;
9769 +
9770 +       s = 1;
9771 +       t = gen;
9772 +       u = exp;
9773 +
9774 +       while (u) {
9775 +               if (u & 1)
9776 +                       s = (s * t) % mod;
9777 +               u >>= 1;
9778 +               t = (t * t) % mod;
9779 +       }
9780 +       return (s);
9781 +}
9782 +
9783 +static void
9784 +ip_initid(void)
9785 +{
9786 +       __u16 j, i;
9787 +       int noprime = 1;
9788 +
9789 +       ru_x = ((tmp = arc4random()) & 0xFFFF) % RU_M;
9790 +
9791 +       ru_seed = (tmp >> 16) & 0x7FFF;
9792 +       ru_seed2 = arc4random() & 0x7FFF;
9793 +
9794 +       ru_b = ((tmp = arc4random()) & 0xfffe) | 1;
9795 +       ru_a = pmod(RU_AGEN, (tmp >> 16) & 0xfffe, RU_M);
9796 +       while (ru_b % 3 == 0)
9797 +               ru_b += 2;
9798 +
9799 +       j = (tmp = arc4random()) % RU_N;
9800 +       tmp = tmp >> 16;
9801 +
9802 +       while (noprime) {
9803 +               for (i = 0; i < PFAC_N; i++)
9804 +                       if (j % pfacts[i] == 0)
9805 +                               break;
9806 +
9807 +               if (i >= PFAC_N)
9808 +                       noprime = 0;
9809 +               else
9810 +                       j = (j + 1) % RU_N;
9811 +       }
9812 +
9813 +       ru_g = pmod(RU_GEN, j, RU_N);
9814 +       ru_counter = 0;
9815 +
9816 +       ru_reseed = xtime.tv_sec + RU_OUT;
9817 +       ru_msb = ru_msb == 0x8000 ? 0 : 0x8000;
9818 +}
9819 +
9820 +__u16
9821 +ip_randomid(void)
9822 +{
9823 +       int i, n;
9824 +
9825 +       if (ru_counter >= RU_MAX || time_after(xtime.tv_sec, ru_reseed))
9826 +               ip_initid();
9827 +
9828 +       if (!tmp)
9829 +               tmp = arc4random();
9830 +
9831 +       n = tmp & 0x3;
9832 +       tmp = tmp >> 2;
9833 +       if (ru_counter + n >= RU_MAX)
9834 +               ip_initid();
9835 +       for (i = 0; i <= n; i++)
9836 +               ru_x = (ru_a * ru_x + ru_b) % RU_M;
9837 +       ru_counter += i;
9838 +
9839 +       return ((ru_seed ^ pmod(ru_g, ru_seed2 ^ ru_x, RU_N)) | ru_msb);
9840 +}
9841 +
9842 +__u16
9843 +tcp_rndiss_encrypt(__u16 val)
9844 +{
9845 +       __u16 sum = 0, i;
9846 +       int cpu = smp_processor_id();
9847 +
9848 +       for (i = 0; i < TCP_RNDISS_ROUNDS; i++) {
9849 +               sum += 0x79b9;
9850 +               val ^= ((__u16) tcp_rndiss_sbox[cpu][(val ^ sum) ^ 0x7f]) << 7;
9851 +               val = ((val & 0xff) << 7) | (val >> 8);
9852 +       }
9853 +
9854 +       return val;
9855 +}
9856 +
9857 +static void
9858 +tcp_rndiss_init(void)
9859 +{
9860 +       int cpu = smp_processor_id();
9861 +
9862 +       get_random_bytes(tcp_rndiss_sbox[cpu], sizeof (tcp_rndiss_sbox));
9863 +       tcp_rndiss_reseed[cpu] = xtime.tv_sec + TCP_RNDISS_OUT;
9864 +       tcp_rndiss_msb[cpu] = tcp_rndiss_msb[cpu] == 0x8000 ? 0 : 0x8000;
9865 +       tcp_rndiss_cnt[cpu] = 0;
9866 +}
9867 +
9868 +__u32
9869 +ip_randomisn(void)
9870 +{
9871 +       int cpu = smp_processor_id();
9872 +
9873 +       if (tcp_rndiss_cnt[cpu] >= TCP_RNDISS_MAX ||
9874 +           time_after(xtime.tv_sec, tcp_rndiss_reseed[cpu]))
9875 +               tcp_rndiss_init();
9876 +
9877 +       return (((tcp_rndiss_encrypt(tcp_rndiss_cnt[cpu]++) |
9878 +                 tcp_rndiss_msb[cpu]) << 16) | (arc4random() & 0x7fff));
9879 +}
9880 diff -urN linux-2.4.21/include/linux/fs.h linux-2.4.21/include/linux/fs.h
9881 --- linux-2.4.21/include/linux/fs.h     2003-06-23 11:41:32.000000000 -0400
9882 +++ linux-2.4.21/include/linux/fs.h     2003-06-23 11:49:17.000000000 -0400
9883 @@ -1070,7 +1070,7 @@
9884  
9885  asmlinkage long sys_open(const char *, int, int);
9886  asmlinkage long sys_close(unsigned int);       /* yes, it's really unsigned */
9887 -extern int do_truncate(struct dentry *, loff_t start);
9888 +extern int do_truncate(struct dentry *, loff_t start, struct vfsmount *);
9889  
9890  extern struct file *filp_open(const char *, int, int);
9891  extern struct file * dentry_open(struct dentry *, struct vfsmount *, int);
9892 diff -urN linux-2.4.21/include/linux/gracl.h linux-2.4.21/include/linux/gracl.h
9893 --- linux-2.4.21/include/linux/gracl.h  1969-12-31 19:00:00.000000000 -0500
9894 +++ linux-2.4.21/include/linux/gracl.h  2003-07-29 17:58:19.000000000 -0400
9895 @@ -0,0 +1,211 @@
9896 +#ifndef GR_ACL_H
9897 +#define GR_ACL_H
9898 +#endif
9899 +#include <linux/grdefs.h>
9900 +#include <linux/resource.h>
9901 +
9902 +#include <asm/resource.h>
9903 +
9904 +/* * * * * * * * * * * * * * * * * * * * *
9905 + * grsecurity ACL System
9906 + * Main header file
9907 + * Purpose: define most gracl data structures 
9908 + * * * * * * * * * * * * * * * * * * * * */
9909 +
9910 +/* Major status information */
9911 +
9912 +#define GR_VERSION  "grsecurity 2.0"
9913 +
9914 +enum {
9915 +
9916 +       SHUTDOWN = 0,
9917 +       ENABLE = 1,
9918 +       SPROLE = 2,
9919 +       RELOAD = 3,
9920 +       SEGVMOD = 4,
9921 +       STATUS = 5
9922 +};
9923 +
9924 +/* Password setup definitions
9925 + * kernel/grhash.c */
9926 +enum {
9927 +       GR_PW_LEN = 128,
9928 +       GR_SALT_LEN = 16,
9929 +       GR_SHA_LEN = 32,
9930 +};
9931 +
9932 +enum {
9933 +       GR_SPROLE_LEN = 64,
9934 +};
9935 +
9936 +/* Begin Data Structures */
9937 +
9938 +struct sprole_pw {
9939 +       unsigned char *rolename;
9940 +       unsigned char salt[GR_SALT_LEN];
9941 +       unsigned char sum[GR_SHA_LEN];  /* 256-bit SHA hash of the password */
9942 +};
9943 +
9944 +struct name_entry {
9945 +       ino_t inode;
9946 +       kdev_t device;
9947 +       char *name;
9948 +       __u16 len;
9949 +};
9950 +
9951 +struct acl_role_db {
9952 +       struct acl_role_label **r_hash;
9953 +       __u32 r_size;
9954 +};
9955 +
9956 +struct name_db {
9957 +       struct name_entry **n_hash;
9958 +       __u32 n_size;
9959 +};
9960 +
9961 +struct crash_uid {
9962 +       uid_t uid;
9963 +       unsigned long expires;
9964 +};
9965 +
9966 +/* Userspace Grsecurity ACL data structures */
9967 +struct acl_subject_label {
9968 +       char *filename;
9969 +       ino_t inode;
9970 +       kdev_t device;
9971 +       __u32 mode;
9972 +       __u32 cap_raise;
9973 +       __u32 cap_lower;
9974 +
9975 +       struct rlimit res[RLIM_NLIMITS + 1];
9976 +       __u16 resmask;
9977 +
9978 +       __u32 ip_proto[8];
9979 +       __u32 ip_type;
9980 +       struct acl_ip_label **ips;
9981 +       __u32 ip_num;
9982 +
9983 +       __u32 crashes;
9984 +       unsigned long expires;
9985 +
9986 +       struct acl_subject_label *parent_subject;
9987 +       struct acl_object_label *proc_object;
9988 +       struct acl_ip_label *ip_object;
9989 +       struct acl_subject_label *prev;
9990 +       struct acl_subject_label *next;
9991 +
9992 +       struct acl_object_label **obj_hash;
9993 +       __u32 obj_hash_size;
9994 +};
9995 +
9996 +struct role_allowed_ip {
9997 +       __u32 addr;
9998 +       __u32 netmask;
9999 +
10000 +       struct role_allowed_ip *prev;
10001 +       struct role_allowed_ip *next;
10002 +};
10003 +
10004 +struct role_transition {
10005 +       char *rolename;
10006 +
10007 +       struct role_transition *prev;
10008 +       struct role_transition *next;
10009 +};
10010 +
10011 +struct acl_role_label {
10012 +       char *rolename;
10013 +       uid_t uidgid;
10014 +       __u16 roletype;
10015 +
10016 +       __u16 auth_attempts;
10017 +       unsigned long expires;
10018 +
10019 +       struct acl_subject_label *root_label;
10020 +       struct acl_subject_label *proc_subject;
10021 +
10022 +       struct acl_role_label *prev;
10023 +       struct acl_role_label *next;
10024 +
10025 +       struct role_transition *transitions;
10026 +       struct role_allowed_ip *allowed_ips;
10027 +       struct acl_subject_label **subj_hash;
10028 +       __u32 subj_hash_size;
10029 +};
10030 +
10031 +struct user_acl_role_db {
10032 +       struct acl_role_label **r_table;
10033 +       __u32 r_entries;        /* number of entries in table */
10034 +       __u32 s_entries;        /* total number of subject acls */
10035 +       __u32 i_entries;        /* total number of ip acls */
10036 +       __u32 o_entries;        /* Total number of object acls */
10037 +       __u32 a_entries;        /* total number of allowed ips */
10038 +       __u32 t_entries;        /* total number of transitions */
10039 +};
10040 +
10041 +struct acl_object_label {
10042 +       char *filename;
10043 +       ino_t inode;
10044 +       kdev_t device;
10045 +       __u32 mode;
10046 +
10047 +       struct acl_subject_label *nested;
10048 +
10049 +       /* next two structures not used */
10050 +
10051 +       struct acl_object_label *prev;
10052 +       struct acl_object_label *next;
10053 +};
10054 +
10055 +struct acl_ip_label {
10056 +       __u32 addr;
10057 +       __u32 netmask;
10058 +       __u16 low, high;
10059 +       __u8 mode;
10060 +       __u32 type;
10061 +       __u32 proto[8];
10062 +
10063 +       /* next two structures not used */
10064 +
10065 +       struct acl_ip_label *prev;
10066 +       struct acl_ip_label *next;
10067 +};
10068 +
10069 +struct gr_arg {
10070 +       struct user_acl_role_db role_db;
10071 +       unsigned char pw[GR_PW_LEN];
10072 +       unsigned char salt[GR_SALT_LEN];
10073 +       unsigned char sum[GR_SHA_LEN];
10074 +       unsigned char sp_role[GR_SPROLE_LEN];
10075 +       struct sprole_pw *sprole_pws;
10076 +       __u16 num_sprole_pws;
10077 +       kdev_t segv_device;
10078 +       ino_t segv_inode;
10079 +       uid_t segv_uid;
10080 +       __u16 mode;
10081 +};
10082 +
10083 +/* End Data Structures Section */
10084 +
10085 +/* Hash functions generated by empirical testing by Brad Spengler
10086 +   Makes good use of the low bits of the inode.  Generally 0-1 times
10087 +   in loop for successful match.  0-3 for unsuccessful match.
10088 +   Shift/add algorithm with modulus of table size and an XOR*/
10089 +
10090 +static __inline__ unsigned long
10091 +rhash(const uid_t uid, const __u16 type, const unsigned long sz)
10092 +{
10093 +       return (((uid << type) + (uid ^ type)) % sz);
10094 +}
10095 +
10096 +static __inline__ unsigned long
10097 +fhash(const ino_t ino, const kdev_t dev, const unsigned long sz)
10098 +{
10099 +       return (((ino + dev) ^ ((ino << 13) + (ino << 23) + (dev << 9))) % sz);
10100 +}
10101 +
10102 +static __inline__ unsigned long
10103 +nhash(const char *name, const __u16 len, const unsigned long sz)
10104 +{
10105 +       return full_name_hash(name, len) % sz;
10106 +}
10107 diff -urN linux-2.4.21/include/linux/gralloc.h linux-2.4.21/include/linux/gralloc.h
10108 --- linux-2.4.21/include/linux/gralloc.h        1969-12-31 19:00:00.000000000 -0500
10109 +++ linux-2.4.21/include/linux/gralloc.h        2003-07-29 17:58:19.000000000 -0400
10110 @@ -0,0 +1,8 @@
10111 +#ifndef __GRALLOC_H
10112 +#define __GRALLOC_H
10113 +
10114 +void acl_free_all(void);
10115 +int acl_alloc_stack_init(unsigned long size);
10116 +void *acl_alloc(unsigned long len);
10117 +
10118 +#endif
10119 diff -urN linux-2.4.21/include/linux/grdefs.h linux-2.4.21/include/linux/grdefs.h
10120 --- linux-2.4.21/include/linux/grdefs.h 1969-12-31 19:00:00.000000000 -0500
10121 +++ linux-2.4.21/include/linux/grdefs.h 2003-06-23 11:49:17.000000000 -0400
10122 @@ -0,0 +1,96 @@
10123 +#ifndef GRDEFS_H
10124 +#define GRDEFS_H
10125 +
10126 +/* Begin grsecurity status declarations */
10127 +
10128 +enum {
10129 +       GR_READY = 0x01,
10130 +       GR_STATUS_INIT = 0x00   // disabled state
10131 +};
10132 +
10133 +/* Begin  ACL declarations */
10134 +
10135 +/* Role flags */
10136 +
10137 +enum {
10138 +       GR_ROLE_USER = 0x0001,
10139 +       GR_ROLE_GROUP = 0x0002,
10140 +       GR_ROLE_DEFAULT = 0x0004,
10141 +       GR_ROLE_SPECIAL = 0x0008,
10142 +       GR_ROLE_AUTH = 0x0010,
10143 +       GR_ROLE_NOPW = 0x0020,
10144 +       GR_ROLE_GOD = 0x0040
10145 +};
10146 +
10147 +/* ACL Subject and Object mode flags */
10148 +enum {
10149 +       GR_DELETED = 0x00000080
10150 +};
10151 +
10152 +/* ACL Object-only mode flags */
10153 +enum {
10154 +       GR_READ = 0x00000001,
10155 +       GR_APPEND = 0x00000002,
10156 +       GR_WRITE = 0x00000004,
10157 +       GR_EXEC = 0x00000008,
10158 +       GR_FIND = 0x00000010,
10159 +       GR_INHERIT = 0x00000040,
10160 +       GR_PTRACERD = 0x00000100,
10161 +       GR_SETID = 0x00000200,
10162 +       GR_CREATE = 0x00000400,
10163 +       GR_DELETE = 0x00000800,
10164 +       GR_AUDIT_READ = 0x00001000,
10165 +       GR_AUDIT_APPEND = 0x00002000,
10166 +       GR_AUDIT_WRITE = 0x0004000,
10167 +       GR_AUDIT_EXEC = 0x00008000,
10168 +       GR_AUDIT_FIND = 0x00010000,
10169 +       GR_AUDIT_INHERIT = 0x00020000,
10170 +       GR_AUDIT_SETID = 0x00400000,
10171 +       GR_AUDIT_CREATE = 0x00800000,
10172 +       GR_AUDIT_DELETE = 0x01000000,
10173 +       GR_SUPPRESS = 0x02000000,
10174 +       GR_NOLEARN = 0x04000000
10175 +};
10176 +
10177 +#define GR_AUDITS (GR_AUDIT_READ | GR_AUDIT_WRITE | GR_AUDIT_APPEND | GR_AUDIT_EXEC | \
10178 +                  GR_AUDIT_FIND | GR_AUDIT_INHERIT | GR_AUDIT_SETID | \
10179 +                  GR_AUDIT_CREATE | GR_AUDIT_DELETE)
10180 +
10181 +/* ACL subject-only mode flags */
10182 +enum {
10183 +       GR_KILL = 0x00000001,
10184 +       GR_VIEW = 0x00000002,
10185 +       GR_PROTECTED = 0x00000100,
10186 +       GR_LEARN = 0x00000200,
10187 +       GR_OVERRIDE = 0x00000400,
10188 +       /* just a placeholder, this mode is only used in userspace */
10189 +       GR_DUMMY = 0x00000800,
10190 +
10191 +       GR_PROTSHM = 0x00040000,
10192 +       GR_KILLPROC = 0x00080000,
10193 +       GR_KILLIPPROC = 0x00100000,
10194 +       /* just a placeholder, this mode is only used in userspace */
10195 +       GR_NOTROJAN = 0x00200000,
10196 +       GR_PROTPROCFD = 0x00400000,
10197 +       GR_PROCACCT = 0x00800000
10198 +};
10199 +
10200 +#define GR_CRASH_RES   11
10201 +#define GR_UIDTABLE_MAX 500
10202 +
10203 +/* begin resource learning section */
10204 +enum {
10205 +       GR_RLIM_CPU_BUMP = 60,
10206 +       GR_RLIM_FSIZE_BUMP = 50000,
10207 +       GR_RLIM_DATA_BUMP = 10000,
10208 +       GR_RLIM_STACK_BUMP = 1000,
10209 +       GR_RLIM_CORE_BUMP = 10000,
10210 +       GR_RLIM_RSS_BUMP = 500000,
10211 +       GR_RLIM_NPROC_BUMP = 1,
10212 +       GR_RLIM_NOFILE_BUMP = 5,
10213 +       GR_RLIM_MEMLOCK_BUMP = 50000,
10214 +       GR_RLIM_AS_BUMP = 500000,
10215 +       GR_RLIM_LOCKS_BUMP = 2
10216 +};
10217 +
10218 +#endif
10219 diff -urN linux-2.4.21/include/linux/grinternal.h linux-2.4.21/include/linux/grinternal.h
10220 --- linux-2.4.21/include/linux/grinternal.h     1969-12-31 19:00:00.000000000 -0500
10221 +++ linux-2.4.21/include/linux/grinternal.h     2003-06-23 11:49:17.000000000 -0400
10222 @@ -0,0 +1,177 @@
10223 +#ifndef __GRINTERNAL_H
10224 +#define __GRINTERNAL_H
10225 +
10226 +#ifdef CONFIG_GRKERNSEC
10227 +
10228 +#include <linux/grdefs.h>
10229 +#include <linux/grmsg.h>
10230 +
10231 +extern void gr_add_learn_entry(const char *fmt, ...);
10232 +extern __u32 gr_search_file(const struct dentry *dentry, const __u32 mode,
10233 +                           const struct vfsmount *mnt);
10234 +extern __u32 gr_check_create(const struct dentry *new_dentry,
10235 +                            const struct dentry *parent,
10236 +                            const struct vfsmount *mnt, const __u32 mode);
10237 +extern int gr_check_protected_task(const struct task_struct *task);
10238 +extern __inline__ __u32 to_gr_audit(const __u32 reqmode);
10239 +extern int gr_handle_rename(struct inode *old_dir, struct inode *new_dir,
10240 +                           struct dentry *old_dentry,
10241 +                           struct dentry *new_dentry,
10242 +                           struct vfsmount *mnt, const __u8 replace);
10243 +extern int gr_set_acls(const int type);
10244 +
10245 +extern void gr_handle_alertkill(void);
10246 +extern char *gr_to_filename(const struct dentry *dentry,
10247 +                           const struct vfsmount *mnt);
10248 +
10249 +extern int grsec_enable_link;
10250 +extern int grsec_enable_fifo;
10251 +extern int grsec_enable_execve;
10252 +extern int grsec_enable_forkbomb;
10253 +extern int grsec_forkbomb_gid;
10254 +extern int grsec_forkbomb_sec;
10255 +extern int grsec_forkbomb_max;
10256 +extern int grsec_enable_execlog;
10257 +extern int grsec_enable_signal;
10258 +extern int grsec_enable_forkfail;
10259 +extern int grsec_enable_time;
10260 +extern int grsec_enable_chroot_shmat;
10261 +extern int grsec_enable_chroot_findtask;
10262 +extern int grsec_enable_chroot_mount;
10263 +extern int grsec_enable_chroot_double;
10264 +extern int grsec_enable_chroot_pivot;
10265 +extern int grsec_enable_chroot_chdir;
10266 +extern int grsec_enable_chroot_chmod;
10267 +extern int grsec_enable_chroot_mknod;
10268 +extern int grsec_enable_chroot_fchdir;
10269 +extern int grsec_enable_chroot_nice;
10270 +extern int grsec_enable_chroot_execlog;
10271 +extern int grsec_enable_chroot_caps;
10272 +extern int grsec_enable_chroot_sysctl;
10273 +extern int grsec_enable_chroot_unix;
10274 +extern int grsec_enable_tpe;
10275 +extern int grsec_tpe_gid;
10276 +extern int grsec_enable_tpe_all;
10277 +extern int grsec_enable_sidcaps;
10278 +extern int grsec_enable_randpid;
10279 +extern int grsec_enable_socket_all;
10280 +extern int grsec_socket_all_gid;
10281 +extern int grsec_enable_socket_client;
10282 +extern int grsec_socket_client_gid;
10283 +extern int grsec_enable_socket_server;
10284 +extern int grsec_socket_server_gid;
10285 +extern int grsec_audit_gid;
10286 +extern int grsec_enable_group;
10287 +extern int grsec_enable_audit_ipc;
10288 +extern int grsec_enable_mount;
10289 +extern int grsec_enable_chdir;
10290 +extern int grsec_lock;
10291 +
10292 +extern struct task_struct *child_reaper;
10293 +
10294 +extern spinlock_t grsec_alert_lock;
10295 +extern unsigned long grsec_alert_wtime;
10296 +extern unsigned long grsec_alert_fyet;
10297 +
10298 +extern spinlock_t grsec_alertgood_lock;
10299 +extern unsigned long grsec_alertgood_wtime;
10300 +extern unsigned long grsec_alertgood_fyet;
10301 +
10302 +extern spinlock_t grsec_audit_lock;
10303 +
10304 +#define proc_is_chrooted(tsk_a)  ((tsk_a->pid > 1) && \
10305 +                         ((tsk_a->fs->root->d_inode->i_dev != \
10306 +                         child_reaper->fs->root->d_inode->i_dev) || \
10307 +                         (tsk_a->fs->root->d_inode->i_ino != \
10308 +                         child_reaper->fs->root->d_inode->i_ino)))
10309 +
10310 +#define have_same_root(tsk_a,tsk_b) ((tsk_a->fs->root->d_inode->i_dev == \
10311 +                         tsk_b->fs->root->d_inode->i_dev) && \
10312 +                         (tsk_a->fs->root->d_inode->i_ino == \
10313 +                         tsk_b->fs->root->d_inode->i_ino))
10314 +
10315 +#define DEFAULTSECARGS current->comm, current->pid, current->uid, \
10316 +                      current->euid, current->gid, current->egid, \
10317 +                      current->p_pptr->comm, current->p_pptr->pid, \
10318 +                      current->p_pptr->uid, current->p_pptr->euid, \
10319 +                      current->p_pptr->gid, current->p_pptr->egid
10320 +
10321 +#define GR_CHROOT_CAPS ( \
10322 +       CAP_TO_MASK(CAP_FOWNER) | \
10323 +       CAP_TO_MASK(CAP_LINUX_IMMUTABLE) | CAP_TO_MASK(CAP_NET_ADMIN) | \
10324 +       CAP_TO_MASK(CAP_SYS_MODULE) | CAP_TO_MASK(CAP_SYS_RAWIO) | \
10325 +       CAP_TO_MASK(CAP_SYS_PACCT) | CAP_TO_MASK(CAP_SYS_ADMIN) | \
10326 +       CAP_TO_MASK(CAP_SYS_BOOT) | CAP_TO_MASK(CAP_SYS_TIME) | \
10327 +       CAP_TO_MASK(CAP_NET_RAW) | CAP_TO_MASK(CAP_SYS_TTY_CONFIG) | \
10328 +       CAP_TO_MASK(CAP_IPC_OWNER))
10329 +
10330 +#define security_alert_good(normal_msg,args...) \
10331 +({ \
10332 +       spin_lock(&grsec_alertgood_lock); \
10333 +       \
10334 +       if (!grsec_alertgood_wtime || jiffies - grsec_alertgood_wtime > CONFIG_GRKERNSEC_FLOODTIME * HZ) { \
10335 +           grsec_alertgood_wtime = jiffies; grsec_alertgood_fyet = 0; \
10336 +           if (current->curr_ip) \
10337 +               printk(KERN_ALERT "grsec: From %u.%u.%u.%u: " normal_msg "\n", NIPQUAD(current->curr_ip) , ## args); \
10338 +           else \
10339 +               printk(KERN_ALERT "grsec: " normal_msg "\n" , ## args); \
10340 +       } else if((jiffies - grsec_alertgood_wtime < CONFIG_GRKERNSEC_FLOODTIME * HZ) && (grsec_alertgood_fyet < CONFIG_GRKERNSEC_FLOODBURST)) { \
10341 +           grsec_alertgood_fyet++; \
10342 +           if (current->curr_ip) \
10343 +               printk(KERN_ALERT "grsec: From %u.%u.%u.%u: " normal_msg "\n", NIPQUAD(current->curr_ip) , ## args); \
10344 +           else \
10345 +               printk(KERN_ALERT "grsec: " normal_msg "\n" , ## args); \
10346 +       } else if (grsec_alertgood_fyet == CONFIG_GRKERNSEC_FLOODBURST) { \
10347 +           grsec_alertgood_wtime = jiffies; grsec_alertgood_fyet++; \
10348 +           printk(KERN_ALERT "grsec: more alerts, logging disabled for " \
10349 +                   "%d seconds\n", CONFIG_GRKERNSEC_FLOODTIME); \
10350 +       } \
10351 +       \
10352 +       spin_unlock(&grsec_alertgood_lock); \
10353 +})
10354 +
10355 +#define security_alert(normal_msg,args...) \
10356 +({ \
10357 +       spin_lock(&grsec_alert_lock); \
10358 +       \
10359 +       if (!grsec_alert_wtime || jiffies - grsec_alert_wtime > CONFIG_GRKERNSEC_FLOODTIME * HZ) { \
10360 +           grsec_alert_wtime = jiffies; grsec_alert_fyet = 0; \
10361 +           if (current->curr_ip) \
10362 +               printk(KERN_ALERT "grsec: From %u.%u.%u.%u: " normal_msg "\n", NIPQUAD(current->curr_ip) , ## args); \
10363 +           else \
10364 +               printk(KERN_ALERT "grsec: " normal_msg "\n" , ## args); \
10365 +       } else if((jiffies - grsec_alert_wtime < CONFIG_GRKERNSEC_FLOODTIME * HZ) && (grsec_alert_fyet < CONFIG_GRKERNSEC_FLOODBURST)) { \
10366 +           grsec_alert_fyet++; \
10367 +           if (current->curr_ip) \
10368 +               printk(KERN_ALERT "grsec: From %u.%u.%u.%u: " normal_msg "\n", NIPQUAD(current->curr_ip) , ## args); \
10369 +           else \
10370 +               printk(KERN_ALERT "grsec: " normal_msg "\n" , ## args); \
10371 +       } else if (grsec_alert_fyet == CONFIG_GRKERNSEC_FLOODBURST) { \
10372 +           grsec_alert_wtime = jiffies; grsec_alert_fyet++; \
10373 +           printk(KERN_ALERT "grsec: more alerts, logging disabled for " \
10374 +                   "%d seconds\n", CONFIG_GRKERNSEC_FLOODTIME); \
10375 +       } \
10376 +       \
10377 +       gr_handle_alertkill(); \
10378 +       spin_unlock(&grsec_alert_lock); \
10379 +})
10380 +
10381 +#define security_audit(normal_msg,args...) \
10382 +({ \
10383 +       spin_lock(&grsec_audit_lock); \
10384 +       if (current->curr_ip) \
10385 +               printk(KERN_INFO "grsec: From %u.%u.%u.%u: " normal_msg "\n", \
10386 +                      NIPQUAD(current->curr_ip) , ## args); \
10387 +       else \
10388 +               printk(KERN_INFO "grsec: " normal_msg "\n", ## args); \
10389 +       spin_unlock(&grsec_audit_lock); \
10390 +})
10391 +
10392 +#define security_learn(normal_msg,args...) \
10393 +({ \
10394 +       gr_add_learn_entry(normal_msg "\n", ## args); \
10395 +})
10396 +
10397 +#endif
10398 +
10399 +#endif
10400 diff -urN linux-2.4.21/include/linux/grmsg.h linux-2.4.21/include/linux/grmsg.h
10401 --- linux-2.4.21/include/linux/grmsg.h  1969-12-31 19:00:00.000000000 -0500
10402 +++ linux-2.4.21/include/linux/grmsg.h  2003-06-23 11:49:17.000000000 -0400
10403 @@ -0,0 +1,101 @@
10404 +#define DEFAULTSECMSG "(%.16s:%d) uid/euid:%d/%d gid/egid:%d/%d, parent (%.16s:%d) uid/euid:%d/%d gid/egid:%d/%d"
10405 +#define GR_ACL_PROCACCT_MSG "(%.16s:%d) IP:%u.%u.%u.%u TTY:%.64s uid/euid:%d/%d gid/egid:%d/%d run time:[%ud %uh %um %us] cpu time:[%ud %uh %um %us] %s with exit code %ld, parent (%.16s:%d) IP:%u.%u.%u.%u TTY:%.64s uid/euid:%d/%d gid/egid:%d/%d"
10406 +#define GR_PTRACE_ACL_MSG "denied ptrace of %.950s(%.16s:%d) by " DEFAULTSECMSG
10407 +#define GR_IOPERM_MSG "denied use of ioperm() by " DEFAULTSECMSG
10408 +#define GR_IOPL_MSG "denied use of iopl() by " DEFAULTSECMSG
10409 +#define GR_SHMAT_ACL_MSG "denied attach of shared memory of UID %u, PID %d, ID %u by " DEFAULTSECMSG
10410 +#define GR_UNIX_CHROOT_MSG "denied connect to abstract AF_UNIX socket outside of chroot by " DEFAULTSECMSG
10411 +#define GR_SHMAT_CHROOT_MSG "denied attach of shared memory outside of chroot by " DEFAULTSECMSG
10412 +#define GR_KMEM_MSG "attempted write to /dev/kmem by " DEFAULTSECMSG
10413 +#define GR_PORT_OPEN_MSG "attempted open of /dev/port by " DEFAULTSECMSG
10414 +#define GR_MEM_WRITE_MSG "attempted write of /dev/mem by " DEFAULTSECMSG
10415 +#define GR_MEM_MMAP_MSG "attempted mmap write of /dev/[k]mem by " DEFAULTSECMSG
10416 +#define GR_SYMLINK_MSG "not following symlink %.950s owned by %d.%d by " DEFAULTSECMSG
10417 +#define GR_LEARN_AUDIT_MSG "%s\t%u\t%u\t%u\t%.4095s\t%.4095s\t%lu\t%lu\t%.4095s\t%lu\t%u.%u.%u.%u"
10418 +#define GR_HIDDEN_ACL_MSG "%s access to hidden file %.950s by " DEFAULTSECMSG
10419 +#define GR_OPEN_ACL_MSG "%s open of %.950s for%s%s by " DEFAULTSECMSG
10420 +#define GR_CREATE_ACL_MSG "%s create of %.950s for%s%s by " DEFAULTSECMSG
10421 +#define GR_FIFO_MSG "denied writing FIFO %.950s of %d.%d by " DEFAULTSECMSG
10422 +#define GR_MKNOD_CHROOT_MSG "refused attempt to mknod %.950s from chroot by " DEFAULTSECMSG
10423 +#define GR_MKNOD_ACL_MSG "%s mknod of %.950s by " DEFAULTSECMSG
10424 +#define GR_UNIXCONNECT_ACL_MSG "%s connect to the unix domain socket %.950s by " DEFAULTSECMSG
10425 +#define GR_MKDIR_ACL_MSG "%s mkdir of %.950s by " DEFAULTSECMSG
10426 +#define GR_RMDIR_ACL_MSG "%s rmdir of %.950s by " DEFAULTSECMSG
10427 +#define GR_UNLINK_ACL_MSG "%s unlink of %.950s by " DEFAULTSECMSG
10428 +#define GR_SYMLINK_ACL_MSG "%s symlink from %.480s to %.480s by " DEFAULTSECMSG
10429 +#define GR_HARDLINK_MSG "denied hardlink of %.930s (owned by %d.%d) to %.30s for " DEFAULTSECMSG
10430 +#define GR_LINK_ACL_MSG "%s link of %.480s to %.480s by " DEFAULTSECMSG
10431 +#define GR_INHERIT_ACL_MSG "successful inherit of %.480s's ACL for %.480s by " DEFAULTSECMSG
10432 +#define GR_RENAME_ACL_MSG "%s rename of %.480s to %.480s by " DEFAULTSECMSG
10433 +#define GR_PTRACE_EXEC_ACL_MSG "denied ptrace of %.950s by " DEFAULTSECMSG
10434 +#define GR_NPROC_MSG "attempt to overstep process limit by " DEFAULTSECMSG
10435 +#define GR_EXEC_ACL_MSG "%s execution of %.950s by " DEFAULTSECMSG
10436 +#define GR_EXEC_TPE_MSG "denied untrusted exec of %.950s by " DEFAULTSECMSG
10437 +#define GR_SEGVSTART_ACL_MSG "possible exploit bruteforcing on " DEFAULTSECMSG " Banning uid %u from login for %lu seconds"
10438 +#define GR_SEGVNOSUID_ACL_MSG "possible exploit bruteforcing on " DEFAULTSECMSG " Banning execution of [%.16s:%lu] for %lu seconds"
10439 +#define GR_MOUNT_CHROOT_MSG "denied attempt to mount %.30s as %.930s from chroot by " DEFAULTSECMSG
10440 +#define GR_PIVOT_CHROOT_MSG "denied attempt to pivot_root from chroot by " DEFAULTSECMSG
10441 +#define GR_TRUNCATE_ACL_MSG "%s truncate of %.950s by " DEFAULTSECMSG
10442 +#define GR_ATIME_ACL_MSG "%s access time change of %.950s by " DEFAULTSECMSG
10443 +#define GR_ACCESS_ACL_MSG "%s access of %.950s for%s%s%s by " DEFAULTSECMSG
10444 +#define GR_CHROOT_CHROOT_MSG "denied attempt to double chroot to %.950s by " DEFAULTSECMSG
10445 +#define GR_FCHMOD_ACL_MSG "%s fchmod of %.950s by " DEFAULTSECMSG
10446 +#define GR_CHMOD_CHROOT_MSG "denied attempt to chmod +s %.950s by " DEFAULTSECMSG
10447 +#define GR_CHMOD_ACL_MSG "%s chmod of %.950s by " DEFAULTSECMSG
10448 +#define GR_CHROOT_FCHDIR_MSG "attempted fchdir outside of chroot to %.950s by " DEFAULTSECMSG
10449 +#define GR_CHOWN_ACL_MSG "%s chown of %.950s by " DEFAULTSECMSG
10450 +#define GR_WRITLIB_ACL_MSG "denied load of writable library %.950s by " DEFAULTSECMSG
10451 +#define GR_INITF_ACL_MSG "init_variables() failed %s"
10452 +#define GR_DISABLED_ACL_MSG "Error loading %s, trying to run kernel with acls disabled. To disable acls at startup use <kernel image name> gracl=off from your boot loader"
10453 +#define GR_DEV_ACL_MSG "/dev/grsec: being fed garbage %d bytes sent %d required"
10454 +#define GR_SHUTS_ACL_MSG "shutdown auth success for " DEFAULTSECMSG
10455 +#define GR_SHUTF_ACL_MSG "shutdown auth failure for " DEFAULTSECMSG
10456 +#define GR_SHUTI_ACL_MSG "ignoring shutdown for disabled RBAC system for " DEFAULTSECMSG
10457 +#define GR_SEGVMODS_ACL_MSG "segvmod auth success for " DEFAULTSECMSG
10458 +#define GR_SEGVMODF_ACL_MSG "segvmod auth failure for " DEFAULTSECMSG
10459 +#define GR_SEGVMODI_ACL_MSG "ignoring segvmod for disabled RBAC system for " DEFAULTSECMSG
10460 +#define GR_ENABLE_ACL_MSG "Loaded %s"
10461 +#define GR_ENABLEF_ACL_MSG "Unable to load %s for " DEFAULTSECMSG " RBAC system may already be enabled."
10462 +#define GR_RELOADI_ACL_MSG "Ignoring reload request for disabled RBAC system"
10463 +#define GR_RELOAD_ACL_MSG "Reloaded %s"
10464 +#define GR_RELOADF_ACL_MSG "Failed reload of %s for " DEFAULTSECMSG
10465 +#define GR_SPROLEI_ACL_MSG "Ignoring change to special role for disabled RBAC system for " DEFAULTSECMSG
10466 +#define GR_SPROLES_ACL_MSG "successful change to special role %s (id %d) by " DEFAULTSECMSG
10467 +#define GR_SPROLEL_ACL_MSG "special role %s (id %d) exited by " DEFAULTSECMSG
10468 +#define GR_SPROLEF_ACL_MSG "special role %s failure for " DEFAULTSECMSG
10469 +#define GR_INVMODE_ACL_MSG "Invalid mode %d by " DEFAULTSECMSG
10470 +#define GR_MAXPW_ACL_MSG "Maximum pw attempts reached (%d), locking password authentication"
10471 +#define GR_MAXROLEPW_ACL_MSG "Maximum pw attempts reached (%d) trying to auth to special role %s, locking auth for role of " DEFAULTSECMSG
10472 +#define GR_PRIORITY_CHROOT_MSG "attempted priority change of process (%.16s:%d) by " DEFAULTSECMSG
10473 +#define GR_CAPSET_CHROOT_MSG "denied capset of (%.16s:%d) within chroot by " DEFAULTSECMSG
10474 +#define GR_FAILFORK_MSG "failed fork with errno %d by " DEFAULTSECMSG
10475 +#define GR_NICE_CHROOT_MSG "attempted priority change by " DEFAULTSECMSG
10476 +#define GR_UNISIGLOG_MSG "signal %d sent to " DEFAULTSECMSG
10477 +#define GR_DUALSIGLOG_MSG "signal %d sent to " DEFAULTSECMSG " by " DEFAULTSECMSG
10478 +#define GR_SIG_ACL_MSG "Attempted send of signal %d to protected task " DEFAULTSECMSG " by " DEFAULTSECMSG
10479 +#define GR_SYSCTL_MSG "attempt to modify grsecurity sysctl value : %.32s by " DEFAULTSECMSG
10480 +#define GR_SYSCTL_ACL_MSG "%s sysctl of %.950s for%s%s by " DEFAULTSECMSG
10481 +#define GR_TIME_MSG "time set by " DEFAULTSECMSG
10482 +#define GR_DEFACL_MSG "Fatal: Unable to find ACL for (%.16s:%d)"
10483 +#define GR_MMAP_ACL_MSG "%s executable mmap of %.950s by " DEFAULTSECMSG
10484 +#define GR_MPROTECT_ACL_MSG "%s executable mprotect of %.950s by " DEFAULTSECMSG
10485 +#define GR_SOCK_MSG "attempted socket(%.16s,%.16s,%.16s) by " DEFAULTSECMSG
10486 +#define GR_BIND_MSG "attempted bind() by " DEFAULTSECMSG
10487 +#define GR_CONNECT_MSG "attempted connect by " DEFAULTSECMSG
10488 +#define GR_BIND_ACL_MSG "attempted bind to %u.%u.%u.%u port %u sock type %.16s protocol %.16s by " DEFAULTSECMSG
10489 +#define GR_CONNECT_ACL_MSG "attempted connect to %u.%u.%u.%u port %u sock type %.16s protocol %.16s by " DEFAULTSECMSG
10490 +#define GR_IP_LEARN_MSG "%s\t%u\t%u\t%u\t%.4095s\t%.4095s\t%u.%u.%u.%u\t%u\t%u\t%u\t%u\t%u.%u.%u.%u"
10491 +#define GR_EXEC_CHROOT_MSG "exec of %.980s within chroot by process " DEFAULTSECMSG
10492 +#define GR_CAP_ACL_MSG "use of %s denied for " DEFAULTSECMSG
10493 +#define GR_REMOUNT_AUDIT_MSG "remount of %.30s by " DEFAULTSECMSG
10494 +#define GR_UNMOUNT_AUDIT_MSG "unmount of %.30s by " DEFAULTSECMSG
10495 +#define GR_MOUNT_AUDIT_MSG "mount %.30s to %.64s by " DEFAULTSECMSG
10496 +#define GR_CHDIR_AUDIT_MSG "chdir to %.980s by " DEFAULTSECMSG
10497 +#define GR_EXEC_AUDIT_MSG "exec of %.930s (%.63s) by " DEFAULTSECMSG
10498 +#define GR_MSGQ_AUDIT_MSG "message queue created by " DEFAULTSECMSG
10499 +#define GR_MSGQR_AUDIT_MSG "message queue of uid:%d euid:%d removed by " DEFAULTSECMSG
10500 +#define GR_SEM_AUDIT_MSG "semaphore created by " DEFAULTSECMSG
10501 +#define GR_SEMR_AUDIT_MSG "semaphore of uid:%d euid:%d removed by " DEFAULTSECMSG
10502 +#define GR_SHM_AUDIT_MSG "shared memory of size %d created by " DEFAULTSECMSG
10503 +#define GR_SHMR_AUDIT_MSG "shared memory of uid:%d euid:%d removed by " DEFAULTSECMSG
10504 +#define GR_RESOURCE_MSG "attempted resource overstep by requesting %lu for %.16s against limit %lu by " DEFAULTSECMSG
10505 diff -urN linux-2.4.21/include/linux/grsecurity.h linux-2.4.21/include/linux/grsecurity.h
10506 --- linux-2.4.21/include/linux/grsecurity.h     1969-12-31 19:00:00.000000000 -0500
10507 +++ linux-2.4.21/include/linux/grsecurity.h     2003-07-29 17:58:19.000000000 -0400
10508 @@ -0,0 +1,172 @@
10509 +#ifndef GR_SECURITY_H
10510 +#define GR_SECURITY_H
10511 +
10512 +extern int gr_pid_is_chrooted(const struct task_struct *p);
10513 +extern int gr_handle_chroot_nice(void);
10514 +extern int gr_handle_chroot_sysctl(const int op);
10515 +extern int gr_handle_chroot_capset(const struct task_struct *target);
10516 +extern int gr_handle_chroot_setpriority(const struct task_struct *p,
10517 +                                       const int niceval);
10518 +extern int gr_chroot_fchdir(struct dentry *u_dentry, struct vfsmount *u_mnt);
10519 +extern int gr_handle_chroot_chroot(const struct dentry *dentry,
10520 +                                  const struct vfsmount *mnt);
10521 +extern void gr_handle_chroot_caps(struct task_struct *task);
10522 +extern void gr_handle_chroot_chdir(struct dentry *dentry, struct vfsmount *mnt);
10523 +extern int gr_handle_chroot_chmod(const struct dentry *dentry,
10524 +                                 const struct vfsmount *mnt, const int mode);
10525 +extern int gr_handle_chroot_mknod(const struct dentry *dentry,
10526 +                                 const struct vfsmount *mnt, const int mode);
10527 +extern int gr_handle_chroot_mount(const struct dentry *dentry,
10528 +                                 const struct vfsmount *mnt,
10529 +                                 const char *dev_name);
10530 +extern int gr_handle_chroot_pivot(void);
10531 +extern int gr_handle_chroot_unix(const pid_t pid);
10532 +
10533 +extern int gr_handle_nproc(void);
10534 +
10535 +extern void gr_handle_ioperm(void);
10536 +extern void gr_handle_iopl(void);
10537 +
10538 +extern int gr_tpe_allow(const struct file *file);
10539 +
10540 +extern int gr_random_pid(spinlock_t * pid_lock);
10541 +
10542 +extern void gr_log_forkfail(const int retval);
10543 +extern void gr_log_timechange(void);
10544 +extern void gr_log_signal(const int sig, const struct task_struct *t);
10545 +extern void gr_log_chdir(const struct dentry *dentry,
10546 +                        const struct vfsmount *mnt);
10547 +extern void gr_log_chroot_exec(const struct dentry *dentry,
10548 +                              const struct vfsmount *mnt);
10549 +extern void gr_handle_exec_args(struct linux_binprm *bprm, char **argv);
10550 +extern void gr_log_remount(const char *devname, const int retval);
10551 +extern void gr_log_unmount(const char *devname, const int retval);
10552 +extern void gr_log_mount(const char *from, const char *to, const int retval);
10553 +extern void gr_log_msgget(const int ret, const int msgflg);
10554 +extern void gr_log_msgrm(const uid_t uid, const uid_t cuid);
10555 +extern void gr_log_semget(const int err, const int semflg);
10556 +extern void gr_log_semrm(const uid_t uid, const uid_t cuid);
10557 +extern void gr_log_shmget(const int err, const int shmflg, const size_t size);
10558 +extern void gr_log_shmrm(const uid_t uid, const uid_t cuid);
10559 +
10560 +extern int gr_handle_follow_link(const struct inode *parent,
10561 +                                const struct inode *inode,
10562 +                                const struct dentry *dentry,
10563 +                                const struct vfsmount *mnt);
10564 +extern int gr_handle_fifo(const struct dentry *dentry,
10565 +                         const struct vfsmount *mnt,
10566 +                         const struct dentry *dir, const int flag,
10567 +                         const int acc_mode);
10568 +extern int gr_handle_hardlink(const struct dentry *dentry,
10569 +                             const struct vfsmount *mnt,
10570 +                             struct inode *inode,
10571 +                             const int mode, const char *to);
10572 +
10573 +extern int gr_is_capable(const int cap);
10574 +extern void gr_learn_resource(const struct task_struct *task, const int limit,
10575 +                             const unsigned long wanted);
10576 +extern void gr_copy_label(struct task_struct *tsk);
10577 +extern void gr_handle_crash(struct task_struct *task, const int sig);
10578 +extern int gr_handle_signal(const struct task_struct *p, const int sig);
10579 +extern int gr_check_crash_uid(const uid_t uid);
10580 +extern int gr_check_protected_task(const struct task_struct *task);
10581 +extern int gr_acl_handle_mmap(const struct file *file,
10582 +                             const unsigned long prot);
10583 +extern int gr_acl_handle_mprotect(const struct file *file,
10584 +                                 const unsigned long prot);
10585 +extern int gr_check_hidden_task(const struct task_struct *tsk);
10586 +extern __u32 gr_acl_handle_truncate(const struct dentry *dentry,
10587 +                                   const struct vfsmount *mnt);
10588 +extern __u32 gr_acl_handle_utime(const struct dentry *dentry,
10589 +                                const struct vfsmount *mnt);
10590 +extern __u32 gr_acl_handle_access(const struct dentry *dentry,
10591 +                                 const struct vfsmount *mnt, const int fmode);
10592 +extern __u32 gr_acl_handle_fchmod(const struct dentry *dentry,
10593 +                                 const struct vfsmount *mnt, mode_t mode);
10594 +extern __u32 gr_acl_handle_chmod(const struct dentry *dentry,
10595 +                                const struct vfsmount *mnt, mode_t mode);
10596 +extern __u32 gr_acl_handle_chown(const struct dentry *dentry,
10597 +                                const struct vfsmount *mnt);
10598 +extern int gr_handle_ptrace_exec(const struct dentry *dentry,
10599 +                                const struct vfsmount *mnt);
10600 +extern int gr_handle_ptrace(struct task_struct *task, const long request);
10601 +extern int gr_handle_mmap(const struct file *filp, const unsigned long prot);
10602 +extern __u32 gr_acl_handle_execve(const struct dentry *dentry,
10603 +                                 const struct vfsmount *mnt);
10604 +extern int gr_check_crash_exec(const struct file *filp);
10605 +extern int gr_acl_is_enabled(void);
10606 +extern void gr_set_kernel_label(struct task_struct *task);
10607 +extern void gr_set_role_label(struct task_struct *task, const uid_t uid,
10608 +                             const gid_t gid);
10609 +extern void gr_set_proc_label(const struct dentry *dentry,
10610 +                             const struct vfsmount *mnt);
10611 +extern __u32 gr_acl_handle_hidden_file(const struct dentry *dentry,
10612 +                                      const struct vfsmount *mnt);
10613 +extern __u32 gr_acl_handle_open(const struct dentry *dentry,
10614 +                               const struct vfsmount *mnt, const int fmode);
10615 +extern __u32 gr_acl_handle_creat(const struct dentry *dentry,
10616 +                                const struct dentry *p_dentry,
10617 +                                const struct vfsmount *p_mnt, const int fmode,
10618 +                                const int imode);
10619 +extern void gr_handle_create(const struct dentry *dentry,
10620 +                            const struct vfsmount *mnt);
10621 +extern __u32 gr_acl_handle_mknod(const struct dentry *new_dentry,
10622 +                                const struct dentry *parent_dentry,
10623 +                                const struct vfsmount *parent_mnt,
10624 +                                const int mode);
10625 +extern __u32 gr_acl_handle_mkdir(const struct dentry *new_dentry,
10626 +                                const struct dentry *parent_dentry,
10627 +                                const struct vfsmount *parent_mnt);
10628 +extern __u32 gr_acl_handle_rmdir(const struct dentry *dentry,
10629 +                                const struct vfsmount *mnt);
10630 +extern void gr_handle_delete(const ino_t ino, const kdev_t dev);
10631 +extern __u32 gr_acl_handle_unlink(const struct dentry *dentry,
10632 +                                 const struct vfsmount *mnt);
10633 +extern __u32 gr_acl_handle_symlink(const struct dentry *new_dentry,
10634 +                                  const struct dentry *parent_dentry,
10635 +                                  const struct vfsmount *parent_mnt,
10636 +                                  const char *from);
10637 +extern __u32 gr_acl_handle_link(const struct dentry *new_dentry,
10638 +                               const struct dentry *parent_dentry,
10639 +                               const struct vfsmount *parent_mnt,
10640 +                               const struct dentry *old_dentry,
10641 +                               const struct vfsmount *old_mnt, const char *to);
10642 +extern int gr_acl_handle_rename(struct dentry *new_dentry,
10643 +                               struct dentry *parent_dentry,
10644 +                               const struct vfsmount *parent_mnt,
10645 +                               struct dentry *old_dentry,
10646 +                               struct inode *old_parent_inode,
10647 +                               struct vfsmount *old_mnt, const char *newname);
10648 +extern __u32 gr_check_link(const struct dentry *new_dentry,
10649 +                          const struct dentry *parent_dentry,
10650 +                          const struct vfsmount *parent_mnt,
10651 +                          const struct dentry *old_dentry,
10652 +                          const struct vfsmount *old_mnt);
10653 +extern __u32 gr_acl_handle_filldir(const struct dentry *dentry,
10654 +                                  const struct vfsmount *mnt, const ino_t ino);
10655 +extern __u32 gr_acl_handle_unix(const struct dentry *dentry,
10656 +                               const struct vfsmount *mnt);
10657 +extern void gr_acl_handle_exit(void);
10658 +extern void gr_acl_handle_psacct(struct task_struct *task, const long code);
10659 +extern int gr_acl_handle_procpidmem(const struct task_struct *task);
10660 +extern __u32 gr_cap_rtnetlink(void);
10661 +
10662 +#ifdef CONFIG_GRKERNSEC
10663 +extern void gr_handle_mem_write(void);
10664 +extern void gr_handle_kmem_write(void);
10665 +extern void gr_handle_open_port(void);
10666 +extern int gr_handle_mem_mmap(const unsigned long offset,
10667 +                             struct vm_area_struct *vma);
10668 +
10669 +extern __u16 ip_randomid(void);
10670 +extern __u32 ip_randomisn(void);
10671 +extern __u32 arc4random(void);
10672 +
10673 +extern int grsec_enable_dmesg;
10674 +extern int grsec_enable_randid;
10675 +extern int grsec_enable_randisn;
10676 +extern int grsec_enable_randsrc;
10677 +extern int grsec_enable_randrpc;
10678 +#endif
10679 +
10680 +#endif
10681 diff -urN linux-2.4.21/include/linux/mm.h linux-2.4.21/include/linux/mm.h
10682 --- linux-2.4.21/include/linux/mm.h     2003-06-23 11:41:32.000000000 -0400
10683 +++ linux-2.4.21/include/linux/mm.h     2003-06-23 11:49:17.000000000 -0400
10684 @@ -22,6 +22,9 @@
10685  extern struct list_head active_list;
10686  extern struct list_head inactive_list;
10687  
10688 +extern void gr_learn_resource(const struct task_struct * task, const int limit,
10689 +                             const unsigned long wanted);
10690 +
10691  #include <asm/page.h>
10692  #include <asm/pgtable.h>
10693  #include <asm/atomic.h>
10694 @@ -639,6 +704,8 @@
10695         address &= PAGE_MASK;
10696         spin_lock(&vma->vm_mm->page_table_lock);
10697         grow = (vma->vm_start - address) >> PAGE_SHIFT;
10698 +       gr_learn_resource(current, RLIMIT_STACK, vma->vm_end - address);
10699 +       gr_learn_resource(current, RLIMIT_AS, (vma->vm_mm->total_vm + grow) << PAGE_SHIFT);
10700         if (vma->vm_end - address > current->rlim[RLIMIT_STACK].rlim_cur ||
10701             ((vma->vm_mm->total_vm + grow) << PAGE_SHIFT) > current->rlim[RLIMIT_AS].rlim_cur) {
10702                 spin_unlock(&vma->vm_mm->page_table_lock);
10703 diff -urN linux-2.4.21/include/linux/proc_fs.h linux-2.4.21/include/linux/proc_fs.h
10704 --- linux-2.4.21/include/linux/proc_fs.h        2003-06-23 11:41:32.000000000 -0400
10705 +++ linux-2.4.21/include/linux/proc_fs.h        2003-06-23 11:49:17.000000000 -0400
10706 @@ -143,6 +143,9 @@
10707  extern struct proc_dir_entry *proc_mknod(const char *,mode_t,
10708                 struct proc_dir_entry *,kdev_t);
10709  extern struct proc_dir_entry *proc_mkdir(const char *,struct proc_dir_entry *);
10710 +#ifdef CONFIG_GRKERNSEC_PROC
10711 +extern struct proc_dir_entry *proc_priv_mkdir(const char *, struct proc_dir_entry *);
10712 +#endif
10713  
10714  static inline struct proc_dir_entry *create_proc_read_entry(const char *name,
10715         mode_t mode, struct proc_dir_entry *base, 
10716 diff -urN linux-2.4.21/include/linux/sched.h linux-2.4.21/include/linux/sched.h
10717 --- linux-2.4.21/include/linux/sched.h  2003-06-23 11:41:32.000000000 -0400
10718 +++ linux-2.4.21/include/linux/sched.h  2003-06-23 11:49:17.000000000 -0400
10719 @@ -27,6 +27,9 @@
10720  #include <linux/securebits.h>
10721  #include <linux/fs_struct.h>
10722  
10723 +extern int gr_is_capable(const int cap);
10724 +extern int gr_pid_is_chrooted(const struct task_struct *p);
10725 +
10726  struct exec_domain;
10727  
10728  /*
10729 @@ -415,6 +432,19 @@
10730  
10731  /* journalling filesystem info */
10732         void *journal_info;
10733 +
10734 +#ifdef CONFIG_GRKERNSEC
10735 +/* added by grsecurity's ACL system */ 
10736 +       struct acl_subject_label *acl;
10737 +       struct acl_role_label *role;
10738 +       struct file *exec_file;
10739 +       u32 curr_ip;
10740 +       u16 acl_role_id;        
10741 +       u8 acl_sp_role:1;       
10742 +       u8 used_accept:1;       
10743 +       u8 used_connect:1;      
10744 +       u8 is_writable:1;
10745 +#endif
10746  };
10747  
10748  /*
10749 @@ -549,6 +586,8 @@
10750         *p->pidhash_pprev = p->pidhash_next;
10751  }
10752  
10753 +#include <asm/current.h>
10754 +
10755  static inline task_t *find_task_by_pid(int pid)
10756  {
10757         task_t *p, **htable = &pidhash[pid_hashfn(pid)];
10758 @@ -556,6 +595,8 @@
10759         for(p = *htable; p && p->pid != pid; p = p->pidhash_next)
10760                 ;
10761  
10762 +       if(gr_pid_is_chrooted(p)) p = NULL;
10763 +
10764         return p;
10765  }
10766  
10767 @@ -576,8 +617,6 @@
10768  extern struct user_struct * alloc_uid(uid_t);
10769  extern void free_uid(struct user_struct *);
10770  
10771 -#include <asm/current.h>
10772 -
10773  extern unsigned long volatile jiffies;
10774  extern unsigned long itimer_ticks;
10775  extern unsigned long itimer_next;
10776 @@ -741,7 +780,7 @@
10777  static inline int capable(int cap)
10778  {
10779  #if 1 /* ok now */
10780 -       if (cap_raised(current->cap_effective, cap))
10781 +       if (cap_raised(current->cap_effective, cap) && gr_is_capable(cap))
10782  #else
10783         if (cap_is_fs_cap(cap) ? current->fsuid == 0 : current->euid == 0)
10784  #endif
10785 diff -urN linux-2.4.21/include/linux/sysctl.h linux-2.4.21/include/linux/sysctl.h
10786 --- linux-2.4.21/include/linux/sysctl.h 2003-06-23 11:41:32.000000000 -0400
10787 +++ linux-2.4.21/include/linux/sysctl.h 2003-06-23 11:49:17.000000000 -0400
10788 @@ -125,6 +125,7 @@
10789         KERN_CADPID=54,         /* int: PID of the process to notify on CAD */
10790         KERN_KDB=55,            /* int: kdb on/off */
10791         KERN_CORE_PATTERN=56,   /* string: pattern for core-files */
10792 +       KERN_GRSECURITY=68,     /* grsecurity */
10793  };
10794  
10795  
10796 diff -urN linux-2.4.21/include/net/inetpeer.h linux-2.4.21/include/net/inetpeer.h
10797 --- linux-2.4.21/include/net/inetpeer.h 2003-06-23 11:41:37.000000000 -0400
10798 +++ linux-2.4.21/include/net/inetpeer.h 2003-06-23 11:49:17.000000000 -0400
10799 @@ -13,6 +13,7 @@
10800  #include <linux/init.h>
10801  #include <linux/sched.h>
10802  #include <linux/spinlock.h>
10803 +
10804  #include <asm/atomic.h>
10805  
10806  struct inet_peer
10807 @@ -34,6 +35,11 @@
10808  /* can be called with or without local BH being disabled */
10809  struct inet_peer       *inet_getpeer(__u32 daddr, int create);
10810  
10811 +#ifdef CONFIG_GRKERNSEC_RANDID
10812 +extern int grsec_enable_randid;
10813 +extern __u16 ip_randomid(void);
10814 +#endif
10815 +
10816  extern spinlock_t inet_peer_unused_lock;
10817  extern struct inet_peer *inet_peer_unused_head;
10818  extern struct inet_peer **inet_peer_unused_tailp;
10819 @@ -58,7 +64,14 @@
10820         __u16 id;
10821  
10822         spin_lock_bh(&inet_peer_idlock);
10823 -       id = p->ip_id_count++;
10824 +
10825 +#ifdef CONFIG_GRKERNSEC_RANDID
10826 +       if(grsec_enable_randid)
10827 +               id = htons(ip_randomid());
10828 +       else
10829 +#endif
10830 +               id = p->ip_id_count++;
10831 +
10832         spin_unlock_bh(&inet_peer_idlock);
10833         return id;
10834  }
10835 diff -urN linux-2.4.21/include/net/ip.h linux-2.4.21/include/net/ip.h
10836 --- linux-2.4.21/include/net/ip.h       2003-06-23 11:41:37.000000000 -0400
10837 +++ linux-2.4.21/include/net/ip.h       2003-06-23 11:49:17.000000000 -0400
10838 @@ -64,6 +64,11 @@
10839         void                    (*destructor)(struct sock *);
10840  };
10841  
10842 +#ifdef CONFIG_GRKERNSEC_RANDID
10843 +extern int grsec_enable_randid;
10844 +extern __u16 ip_randomid(void);
10845 +#endif
10846 +
10847  extern struct ip_ra_chain *ip_ra_chain;
10848  extern rwlock_t ip_ra_lock;
10849  
10850 @@ -196,7 +201,13 @@
10851                  * does not change, they drop every other packet in
10852                  * a TCP stream using header compression.
10853                  */
10854 -               iph->id = ((sk && sk->daddr) ? htons(sk->protinfo.af_inet.id++) : 0);
10855 +
10856 +#ifdef CONFIG_GRKERNSEC_RANDID
10857 +               if(grsec_enable_randid)
10858 +                       iph->id = htons(ip_randomid());
10859 +               else
10860 +#endif
10861 +                       iph->id = ((sk && sk->daddr) ? htons(sk->protinfo.af_inet.id++) : 0);
10862         } else
10863                 __ip_select_ident(iph, dst);
10864  }
10865 diff -urN linux-2.4.21/init/main.c linux-2.4.21/init/main.c
10866 --- linux-2.4.21/init/main.c    2003-06-23 11:41:31.000000000 -0400
10867 +++ linux-2.4.21/init/main.c    2003-06-23 11:49:17.000000000 -0400
10868 @@ -27,6 +27,7 @@
10869  #include <linux/iobuf.h>
10870  #include <linux/bootmem.h>
10871  #include <linux/tty.h>
10872 +#include <linux/grsecurity.h>
10873  
10874  #include <asm/io.h>
10875  #include <asm/bugs.h>
10876 @@ -107,6 +108,8 @@
10877  extern void ipc_init(void);
10878  #endif
10879  
10880 +extern void grsecurity_init(void);
10881 +
10882  /*
10883   * Boot command-line arguments
10884   */
10885 @@ -546,6 +549,7 @@
10886         do_basic_setup();
10887  
10888         prepare_namespace();
10889 +       grsecurity_init();
10890  
10891         /*
10892          * Ok, we have completed the initial bootup, and
10893 diff -urN linux-2.4.21/ipc/msg.c linux-2.4.21/ipc/msg.c
10894 --- linux-2.4.21/ipc/msg.c      2003-06-23 11:41:59.000000000 -0400
10895 +++ linux-2.4.21/ipc/msg.c      2003-06-23 11:49:17.000000000 -0400
10896 @@ -22,6 +22,7 @@
10897  #include <linux/init.h>
10898  #include <linux/proc_fs.h>
10899  #include <linux/list.h>
10900 +#include <linux/grsecurity.h>
10901  #include <asm/uaccess.h>
10902  #include "util.h"
10903  
10904 @@ -326,6 +327,9 @@
10905                 msg_unlock(id);
10906         }
10907         up(&msg_ids.sem);
10908 +
10909 +       gr_log_msgget(ret, msgflg);
10910 +
10911         return ret;
10912  }
10913  
10914 @@ -560,6 +564,8 @@
10915                 break;
10916         }
10917         case IPC_RMID:
10918 +               gr_log_msgrm(ipcp->uid, ipcp->cuid);
10919 +
10920                 freeque (msqid); 
10921                 break;
10922         }
10923 diff -urN linux-2.4.21/ipc/sem.c linux-2.4.21/ipc/sem.c
10924 --- linux-2.4.21/ipc/sem.c      2003-06-23 11:41:59.000000000 -0400
10925 +++ linux-2.4.21/ipc/sem.c      2003-06-23 11:49:17.000000000 -0400
10926 @@ -62,6 +62,7 @@
10927  #include <linux/spinlock.h>
10928  #include <linux/init.h>
10929  #include <linux/proc_fs.h>
10930 +#include <linux/grsecurity.h>
10931  #include <asm/uaccess.h>
10932  #include "util.h"
10933  
10934 @@ -181,6 +182,9 @@
10935         }
10936  
10937         up(&sem_ids.sem);
10938 +
10939 +       gr_log_semget(err, semflg);
10940 +
10941         return err;
10942  }
10943  
10944 @@ -728,6 +732,8 @@
10945  
10946         switch(cmd){
10947         case IPC_RMID:
10948 +               gr_log_semrm(ipcp->uid, ipcp->cuid);
10949 +
10950                 freeary(semid);
10951                 err = 0;
10952                 break;
10953 diff -urN linux-2.4.21/ipc/shm.c linux-2.4.21/ipc/shm.c
10954 --- linux-2.4.21/ipc/shm.c      2003-06-23 11:41:59.000000000 -0400
10955 +++ linux-2.4.21/ipc/shm.c      2003-06-23 11:49:17.000000000 -0400
10956 @@ -23,6 +23,7 @@
10957  #include <linux/mman.h>
10958  #include <linux/proc_fs.h>
10959  #include <asm/uaccess.h>
10960 +#include <linux/grsecurity.h>
10961  
10962  #include "util.h"
10963  
10964 @@ -38,8 +39,21 @@
10965         time_t                  shm_ctim;
10966         pid_t                   shm_cprid;
10967         pid_t                   shm_lprid;
10968 +
10969 +#ifdef CONFIG_GRKERNSEC
10970 +       time_t                  shm_createtime;
10971 +       pid_t                   shm_lapid;
10972 +#endif
10973  };
10974  
10975 +#ifdef CONFIG_GRKERNSEC
10976 +extern int gr_handle_shmat(const pid_t shm_cprid, const pid_t shm_lapid,
10977 +                           const time_t shm_createtime, const uid_t cuid,
10978 +                           const int shmid);
10979 +extern int gr_chroot_shmat(const pid_t shm_cprid, const pid_t shm_lapid,
10980 +                    const time_t shm_createtime);
10981 +#endif
10982 +
10983  #define shm_flags      shm_perm.mode
10984  
10985  static struct file_operations shm_file_operations;
10986 @@ -209,6 +223,9 @@
10987         shp->shm_lprid = 0;
10988         shp->shm_atim = shp->shm_dtim = 0;
10989         shp->shm_ctim = CURRENT_TIME;
10990 +#ifdef CONFIG_GRKERNSEC
10991 +       shp->shm_createtime = CURRENT_TIME;
10992 +#endif
10993         shp->shm_segsz = size;
10994         shp->shm_nattch = 0;
10995         shp->id = shm_buildid(id,shp->shm_perm.seq);
10996 @@ -254,6 +271,9 @@
10997                 shm_unlock(id);
10998         }
10999         up(&shm_ids.sem);
11000 +
11001 +       gr_log_shmget(err, shmflg, size);
11002 +
11003         return err;
11004  }
11005  
11006 @@ -509,6 +529,9 @@
11007                         err=-EPERM;
11008                         goto out_unlock_up;
11009                 }
11010 +
11011 +               gr_log_shmrm(shp->shm_perm.uid, shp->shm_perm.cuid);
11012 +
11013                 if (shp->shm_nattch){
11014                         shp->shm_flags |= SHM_DEST;
11015                         /* Do not find it any more */
11016 @@ -622,9 +645,28 @@
11017                 shm_unlock(shmid);
11018                 return -EACCES;
11019         }
11020 +
11021 +#ifdef CONFIG_GRKERNSEC
11022 +       if (!gr_handle_shmat(shp->shm_cprid, shp->shm_lapid, shp->shm_createtime,
11023 +                            shp->shm_perm.cuid, shmid)) {
11024 +               shm_unlock(shmid);
11025 +               return -EACCES;
11026 +       }
11027 +
11028 +       if (!gr_chroot_shmat(shp->shm_cprid, shp->shm_lapid, shp->shm_createtime)) {
11029 +               shm_unlock(shmid);
11030 +               return -EACCES;
11031 +       }
11032 +#endif
11033 +
11034         file = shp->shm_file;
11035         size = file->f_dentry->d_inode->i_size;
11036         shp->shm_nattch++;
11037 +
11038 +#ifdef CONFIG_GRKERNSEC
11039 +       shp->shm_lapid = current->pid;
11040 +#endif
11041 +
11042         shm_unlock(shmid);
11043  
11044         down_write(&current->mm->mmap_sem);
11045 diff -urN linux-2.4.21/kernel/capability.c linux-2.4.21/kernel/capability.c
11046 --- linux-2.4.21/kernel/capability.c    2003-06-23 11:41:31.000000000 -0400
11047 +++ linux-2.4.21/kernel/capability.c    2003-06-23 11:49:17.000000000 -0400
11048 @@ -7,6 +7,7 @@
11049  
11050  #include <linux/mm.h>
11051  #include <asm/uaccess.h>
11052 +#include <linux/grsecurity.h>
11053  
11054  kernel_cap_t cap_bset = CAP_INIT_EFF_SET;
11055  
11056 @@ -168,6 +169,10 @@
11057               target = current;
11058       }
11059  
11060 +     if (gr_handle_chroot_capset(target)) {
11061 +            error = -ESRCH;
11062 +            goto out;
11063 +     }
11064  
11065       /* verify restrictions on target's new Inheritable set */
11066       if (!cap_issubset(inheritable,
11067 diff -urN linux-2.4.21/kernel/exit.c linux-2.4.21/kernel/exit.c
11068 --- linux-2.4.21/kernel/exit.c  2003-06-23 11:41:31.000000000 -0400
11069 +++ linux-2.4.21/kernel/exit.c  2003-06-23 11:49:17.000000000 -0400
11070 @@ -16,6 +16,7 @@
11071  #ifdef CONFIG_BSD_PROCESS_ACCT
11072  #include <linux/acct.h>
11073  #endif
11074 +#include <linux/grsecurity.h>
11075  
11076  #include <asm/uaccess.h>
11077  #include <asm/pgtable.h>
11078 @@ -165,12 +165,21 @@
11079  {
11080         write_lock_irq(&tasklist_lock);
11081  
11082 +#ifdef CONFIG_GRKERNSEC
11083 +       if (current->exec_file) {
11084 +               fput(current->exec_file);
11085 +               current->exec_file = NULL;
11086 +       }
11087 +#endif
11088 +
11089         /* Reparent to init */
11090         REMOVE_LINKS(current);
11091         current->p_pptr = child_reaper;
11092         current->p_opptr = child_reaper;
11093         SET_LINKS(current);
11094  
11095 +       gr_set_kernel_label(current);
11096 +
11097         /* Set the exit signal to SIGCHLD so we signal init on exit */
11098         current->exit_signal = SIGCHLD;
11099  
11100 @@ -439,6 +440,10 @@
11101  #ifdef CONFIG_BSD_PROCESS_ACCT
11102         acct_process(code);
11103  #endif
11104 +
11105 +       gr_acl_handle_psacct(tsk, code);
11106 +       gr_acl_handle_exit();
11107 +
11108         __exit_mm(tsk);
11109  
11110         lock_kernel();
11111 diff -urN linux-2.4.21/kernel/fork.c linux-2.4.21/kernel/fork.c
11112 --- linux-2.4.21/kernel/fork.c  2003-06-23 11:41:31.000000000 -0400
11113 +++ linux-2.4.21/kernel/fork.c  2003-06-23 11:49:17.000000000 -0400
11114 @@ -22,6 +22,7 @@
11115  #include <linux/personality.h>
11116  #include <linux/compiler.h>
11117  #include <linux/mman.h>
11118 +#include <linux/grsecurity.h>
11119  
11120  #include <asm/pgtable.h>
11121  #include <asm/pgalloc.h>
11122 @@ -93,6 +94,10 @@
11123         if (flags & CLONE_PID)
11124                 return current->pid;
11125  
11126 +       pid = gr_random_pid(&lastpid_lock);
11127 +       if (pid)
11128 +               return pid;
11129 +
11130         spin_lock(&lastpid_lock);
11131         beginpid = last_pid;
11132         if((++last_pid) & 0xffff8000) {
11133 @@ -635,6 +640,9 @@
11134          * friends to set the per-user process limit to something lower
11135          * than the amount of processes root is running. -- Rik
11136          */
11137 +
11138 +       gr_learn_resource(p, RLIMIT_NPROC, atomic_read(&p->user->processes));
11139 +
11140         if (atomic_read(&p->user->processes) >= p->rlim[RLIMIT_NPROC].rlim_cur
11141                       && !capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RESOURCE))
11142                 goto bad_fork_free;
11143 @@ -719,6 +727,7 @@
11144         retval = copy_thread(0, clone_flags, stack_start, stack_size, p, regs);
11145         if (retval)
11146                 goto bad_fork_cleanup_namespace;
11147 +       gr_copy_label(p);
11148         p->semundo = NULL;
11149         
11150         /* Our parent execution domain becomes current domain
11151 @@ -804,6 +813,9 @@
11152         free_uid(p->user);
11153  bad_fork_free:
11154         free_task_struct(p);
11155 +
11156 +       gr_log_forkfail(retval);
11157 +
11158         goto fork_out;
11159  }
11160  
11161 diff -urN linux-2.4.21/kernel/ksyms.c linux-2.4.21/kernel/ksyms.c
11162 --- linux-2.4.21/kernel/ksyms.c 2003-06-23 11:41:31.000000000 -0400
11163 +++ linux-2.4.21/kernel/ksyms.c 2003-06-23 11:49:17.000000000 -0400
11164 @@ -48,6 +48,7 @@
11165  #include <linux/completion.h>
11166  #include <linux/seq_file.h>
11167  #include <linux/dnotify.h>
11168 +#include <linux/grsecurity.h>
11169  #include <asm/checksum.h>
11170  
11171  #if defined(CONFIG_PROC_FS)
11172 @@ -585,3 +586,9 @@
11173  
11174  /* debug */
11175  EXPORT_SYMBOL(dump_stack);
11176 +
11177 +/* grsecurity */
11178 +EXPORT_SYMBOL(gr_is_capable);
11179 +EXPORT_SYMBOL(gr_pid_is_chrooted);
11180 +EXPORT_SYMBOL(gr_learn_resource);
11181 +EXPORT_SYMBOL(gr_set_kernel_label);
11182 diff -urN linux-2.4.21/kernel/module.c linux-2.4.21/kernel/module.c
11183 --- linux-2.4.21/kernel/module.c        2003-06-23 11:41:31.000000000 -0400
11184 +++ linux-2.4.21/kernel/module.c        2003-06-23 11:49:17.000000000 -0400
11185 @@ -887,6 +887,11 @@
11186         struct module *mod;
11187         int err;
11188  
11189 +#ifdef CONFIG_GRKERNSEC_HIDESYM
11190 +       if (!capable(CAP_SYS_MODULE))
11191 +               return -EPERM;
11192 +#endif
11193 +
11194         lock_kernel();
11195         if (name_user == NULL)
11196                 mod = &kernel_module;
11197 @@ -956,6 +961,11 @@
11198         int i;
11199         struct kernel_sym ksym;
11200  
11201 +#ifdef CONFIG_GRKERNSEC_HIDESYM
11202 +       if (!capable(CAP_SYS_MODULE))
11203 +               return 0;
11204 +#endif
11205 +
11206         lock_kernel();
11207         for (mod = module_list, i = 0; mod; mod = mod->next) {
11208                 /* include the count for the module name! */
11209 diff -urN linux-2.4.21/kernel/printk.c linux-2.4.21/kernel/printk.c
11210 --- linux-2.4.21/kernel/printk.c        2003-06-23 11:41:31.000000000 -0400
11211 +++ linux-2.4.21/kernel/printk.c        2003-06-23 11:49:17.000000000 -0400
11212 @@ -26,6 +26,7 @@
11213  #include <linux/interrupt.h>                   /* For in_interrupt() */
11214  #include <linux/config.h>
11215  #include <linux/delay.h>
11216 +#include <linux/grsecurity.h>
11217  
11218  #include <asm/uaccess.h>
11219  
11220 @@ -294,6 +295,11 @@
11221  
11222  asmlinkage long sys_syslog(int type, char * buf, int len)
11223  {
11224 +#ifdef CONFIG_GRKERNSEC_DMESG
11225 +       if (!capable(CAP_SYS_ADMIN) && grsec_enable_dmesg)
11226 +               return -EPERM;
11227 +       else
11228 +#endif
11229         if ((type != 3) && !capable(CAP_SYS_ADMIN))
11230                 return -EPERM;
11231         return do_syslog(type, buf, len);
11232 diff -urN linux-2.4.21/kernel/sched.c linux-2.4.21/kernel/sched.c
11233 --- linux-2.4.21/kernel/sched.c 2003-06-23 11:41:31.000000000 -0400
11234 +++ linux-2.4.21/kernel/sched.c 2003-06-23 11:49:17.000000000 -0400
11235 @@ -29,11 +30,13 @@
11236  #include <linux/nmi.h>
11237  #include <linux/interrupt.h>
11238  #include <linux/init.h>
11239 +#include <linux/file.h>
11240  #include <asm/uaccess.h>
11241  #include <linux/smp_lock.h>
11242  #include <asm/mmu_context.h>
11243  #include <linux/kernel_stat.h>
11244  #include <linux/completion.h>
11245 +#include <linux/grsecurity.h>
11246  
11247  /*
11248   * Convert user-nice values [ -20 ... 0 ... 19 ]
11249 @@ -910,6 +912,9 @@
11250                         return -EPERM;
11251                 if (increment < -40)
11252                         increment = -40;
11253 +
11254 +               if (gr_handle_chroot_nice())
11255 +                       return -EPERM;
11256         }
11257         if (increment > 40)
11258                 increment = 40;
11259 diff -urN linux-2.4.21/kernel/signal.c linux-2.4.21/kernel/signal.c
11260 --- linux-2.4.21/kernel/signal.c        2003-06-23 11:41:31.000000000 -0400
11261 +++ linux-2.4.21/kernel/signal.c        2003-06-23 11:49:17.000000000 -0400
11262 @@ -13,6 +13,8 @@
11263  #include <linux/smp_lock.h>
11264  #include <linux/init.h>
11265  #include <linux/sched.h>
11266 +#include <linux/fs.h>
11267 +#include <linux/grsecurity.h>
11268  
11269  #include <asm/uaccess.h>
11270  
11271 @@ -554,6 +556,8 @@
11272         if (!sig || !t->sig)
11273                 goto out_nolock;
11274  
11275 +       gr_log_signal(sig, t);
11276 +
11277         spin_lock_irqsave(&t->sigmask_lock, flags);
11278         handle_stop_signal(sig, t);
11279  
11280 @@ -603,6 +607,8 @@
11281         recalc_sigpending(t);
11282         spin_unlock_irqrestore(&t->sigmask_lock, flags);
11283  
11284 +       gr_handle_crash(t, sig);
11285 +
11286         return send_sig_info(sig, info, t);
11287  }
11288  
11289 @@ -622,9 +628,13 @@
11290                 read_lock(&tasklist_lock);
11291                 for_each_task(p) {
11292                         if (p->pgrp == pgrp && thread_group_leader(p)) {
11293 -                               int err = send_sig_info(sig, info, p);
11294 -                               if (retval)
11295 -                                       retval = err;
11296 +                               if (gr_handle_signal(p, sig))
11297 +                                       retval = -EPERM;
11298 +                               else {
11299 +                                       int err = send_sig_info(sig, info, p);
11300 +                                       if (retval)
11301 +                                               retval = err;
11302 +                               }
11303                         }
11304                 }
11305                 read_unlock(&tasklist_lock);
11306 @@ -675,7 +685,10 @@
11307                         if (tg)
11308                                 p = tg;
11309                  }
11310 -               error = send_sig_info(sig, info, p);
11311 +               if (gr_handle_signal(p, sig))
11312 +                       error = -EPERM;
11313 +               else
11314 +                       error = send_sig_info(sig, info, p);
11315         }
11316         read_unlock(&tasklist_lock);
11317         return error;
11318 @@ -700,10 +713,14 @@
11319                 read_lock(&tasklist_lock);
11320                 for_each_task(p) {
11321                         if (p->pid > 1 && p != current && thread_group_leader(p)) {
11322 -                               int err = send_sig_info(sig, info, p);
11323 -                               ++count;
11324 -                               if (err != -EPERM)
11325 -                                       retval = err;
11326 +                               if (gr_handle_signal(p, sig))
11327 +                                       retval = -EPERM;
11328 +                               else {
11329 +                                       int err = send_sig_info(sig, info, p);
11330 +                                       ++count;
11331 +                                       if (err != -EPERM)
11332 +                                               retval = err;
11333 +                               }
11334                         }
11335                 }
11336                 read_unlock(&tasklist_lock);
11337 diff -urN linux-2.4.21/kernel/sys.c linux-2.4.21/kernel/sys.c
11338 --- linux-2.4.21/kernel/sys.c   2003-06-23 11:41:31.000000000 -0400
11339 +++ linux-2.4.21/kernel/sys.c   2003-06-23 11:49:17.000000000 -0400
11340 @@ -4,6 +4,7 @@
11341   *  Copyright (C) 1991, 1992  Linus Torvalds
11342   */
11343  
11344 +#include <linux/config.h>
11345  #include <linux/module.h>
11346  #include <linux/mm.h>
11347  #include <linux/utsname.h>
11348 @@ -14,6 +15,7 @@
11349  #include <linux/prctl.h>
11350  #include <linux/init.h>
11351  #include <linux/highuid.h>
11352 +#include <linux/grsecurity.h>
11353  
11354  #include <asm/uaccess.h>
11355  #include <asm/io.h>
11356 @@ -239,6 +241,12 @@
11357                 }
11358                 if (error == -ESRCH)
11359                         error = 0;
11360 +
11361 +               if (gr_handle_chroot_setpriority(p, niceval)) {
11362 +                       read_unlock(&tasklist_lock);
11363 +                       return -ESRCH;
11364 +               }
11365 +
11366                 if (niceval < task_nice(p) && !capable(CAP_SYS_NICE))
11367                         error = -EACCES;
11368                 else
11369 @@ -425,6 +433,9 @@
11370         if (rgid != (gid_t) -1 ||
11371             (egid != (gid_t) -1 && egid != old_rgid))
11372                 current->sgid = new_egid;
11373 +
11374 +       gr_set_role_label(current, current->uid, new_rgid);
11375 +
11376         current->fsgid = new_egid;
11377         current->egid = new_egid;
11378         current->gid = new_rgid;
11379 @@ -447,6 +458,9 @@
11380                         current->mm->dumpable=0;
11381                         wmb();
11382                 }
11383 +
11384 +               gr_set_role_label(current, current->uid, gid);
11385 +
11386                 current->gid = current->egid = current->sgid = current->fsgid = gid;
11387         }
11388         else if ((gid == current->gid) || (gid == current->sgid))
11389 @@ -530,6 +544,9 @@
11390                 current->mm->dumpable = 0;
11391                 wmb();
11392         }
11393 +
11394 +       gr_set_role_label(current, new_ruid, current->gid);
11395 +
11396         current->uid = new_ruid;
11397         current->user = new_user;
11398         free_uid(old_user);
11399 @@ -626,6 +643,9 @@
11400         } else if ((uid != current->uid) && (uid != new_suid))
11401                 return -EPERM;
11402  
11403 +       if (gr_check_crash_uid(uid))
11404 +               return -EPERM;
11405 +
11406         if (old_euid != uid)
11407         {
11408                 current->mm->dumpable = 0;
11409 @@ -722,8 +742,10 @@
11410                 current->egid = egid;
11411         }
11412         current->fsgid = current->egid;
11413 -       if (rgid != (gid_t) -1)
11414 +       if (rgid != (gid_t) -1) {
11415 +               gr_set_role_label(current, current->uid, rgid);
11416                 current->gid = rgid;
11417 +       }
11418         if (sgid != (gid_t) -1)
11419                 current->sgid = sgid;
11420         return 0;
11421 @@ -1140,6 +1162,10 @@
11422         if(copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
11423                 return -EFAULT;
11424         old_rlim = current->rlim + resource;
11425 +
11426 +       if (old_rlim->rlim_max < old_rlim->rlim_cur)
11427 +               return -EINVAL;
11428 +
11429         if (((new_rlim.rlim_cur > old_rlim->rlim_max) ||
11430              (new_rlim.rlim_max > old_rlim->rlim_max)) &&
11431             !capable(CAP_SYS_RESOURCE))
11432 diff -urN linux-2.4.21/kernel/sysctl.c linux-2.4.21/kernel/sysctl.c
11433 --- linux-2.4.21/kernel/sysctl.c        2003-06-23 11:41:31.000000000 -0400
11434 +++ linux-2.4.21/kernel/sysctl.c        2003-06-23 11:49:17.000000000 -0400
11435 @@ -38,6 +38,13 @@
11436  #endif
11437  
11438  #if defined(CONFIG_SYSCTL)
11439 +#include <linux/grsecurity.h>
11440 +#include <linux/grinternal.h>
11441 +
11442 +extern __u32 gr_handle_sysctl(const ctl_table * table, const void *oldval,
11443 +                             const void *newval);
11444 +extern int gr_handle_sysctl_mod(const char *dirname, const char *name, const int op);
11445 +extern int gr_handle_chroot_sysctl(const int op);
11446  
11447  /* External variables not in a header file. */
11448  extern int panic_timeout;
11449 @@ -119,6 +126,8 @@
11450  static ctl_table dev_table[];
11451  extern ctl_table random_table[];
11452  
11453 +static ctl_table grsecurity_table[];
11454 +
11455  /* /proc declarations: */
11456  
11457  #ifdef CONFIG_PROC_FS
11458 @@ -259,8 +268,191 @@
11459         {KERN_S390_USER_DEBUG_LOGGING,"userprocess_debug",
11460          &sysctl_userprocess_debug,sizeof(int),0644,NULL,&proc_dointvec},
11461  #endif
11462 +#ifdef CONFIG_GRKERNSEC_SYSCTL
11463 +       {KERN_GRSECURITY, "grsecurity", NULL, 0, 0555, grsecurity_table},
11464 +#endif
11465 +       {0}
11466 +};
11467 +
11468 +#ifdef CONFIG_GRKERNSEC_SYSCTL
11469 +enum {GS_LINK=1, GS_FIFO, GS_EXECVE, GS_EXECLOG, GS_SIGNAL, 
11470 +GS_FORKFAIL, GS_TIME, GS_CHROOT_SHMAT, GS_CHROOT_UNIX, GS_CHROOT_MNT,
11471 +GS_CHROOT_FCHDIR, GS_CHROOT_DBL, GS_CHROOT_PVT, GS_CHROOT_CD, GS_CHROOT_CM,
11472 +GS_CHROOT_MK, GS_CHROOT_NI, GS_CHROOT_EXECLOG, GS_CHROOT_CAPS, 
11473 +GS_CHROOT_SYSCTL, GS_TPE, GS_TPE_GID, GS_TPE_ALL, GS_SIDCAPS, 
11474 +GS_RANDPID, GS_RANDID, GS_RANDSRC, GS_RANDISN,
11475 +GS_SOCKET_ALL, GS_SOCKET_ALL_GID, GS_SOCKET_CLIENT,
11476 +GS_SOCKET_CLIENT_GID, GS_SOCKET_SERVER, GS_SOCKET_SERVER_GID, GS_TTY, GS_TTYS,
11477 +GS_PTY, GS_GROUP, GS_GID, GS_ACHDIR, GS_AMOUNT, GS_AIPC, GS_DMSG, GS_RANDRPC,
11478 +GS_FINDTASK, GS_LOCK};
11479 +
11480 +static ctl_table grsecurity_table[] = {
11481 +#ifdef CONFIG_GRKERNSEC_LINK
11482 +       {GS_LINK, "linking_restrictions", &grsec_enable_link, sizeof (int),
11483 +        0600, NULL, &proc_dointvec}, 
11484 +#endif
11485 +#ifdef CONFIG_GRKERNSEC_FIFO
11486 +       {GS_FIFO, "fifo_restrictions", &grsec_enable_fifo, sizeof (int),
11487 +        0600, NULL, &proc_dointvec},
11488 +#endif
11489 +#ifdef CONFIG_GRKERNSEC_EXECVE
11490 +       {GS_EXECVE, "execve_limiting", &grsec_enable_execve, sizeof (int),
11491 +        0600, NULL, &proc_dointvec},
11492 +#endif
11493 +#ifdef CONFIG_GRKERNSEC_EXECLOG
11494 +       {GS_EXECLOG, "exec_logging", &grsec_enable_execlog, sizeof (int),
11495 +        0600, NULL, &proc_dointvec},
11496 +#endif
11497 +#ifdef CONFIG_GRKERNSEC_SIGNAL
11498 +       {GS_SIGNAL, "signal_logging", &grsec_enable_signal, sizeof (int),
11499 +        0600, NULL, &proc_dointvec},
11500 +#endif
11501 +#ifdef CONFIG_GRKERNSEC_FORKFAIL
11502 +       {GS_FORKFAIL, "forkfail_logging", &grsec_enable_forkfail, sizeof (int),
11503 +        0600, NULL, &proc_dointvec},
11504 +#endif
11505 +#ifdef CONFIG_GRKERNSEC_TIME
11506 +       {GS_TIME, "timechange_logging", &grsec_enable_time, sizeof (int),
11507 +        0600, NULL, &proc_dointvec},
11508 +#endif
11509 +#ifdef CONFIG_GRKERNSEC_CHROOT_SHMAT
11510 +       {GS_CHROOT_SHMAT, "chroot_deny_shmat", &grsec_enable_chroot_shmat, sizeof (int),
11511 +        0600, NULL, &proc_dointvec},
11512 +#endif
11513 +#ifdef CONFIG_GRKERNSEC_CHROOT_UNIX
11514 +       {GS_CHROOT_UNIX, "chroot_deny_unix", &grsec_enable_chroot_unix, sizeof(int),
11515 +        0600, NULL, &proc_dointvec},
11516 +#endif
11517 +#ifdef CONFIG_GRKERNSEC_CHROOT_MOUNT
11518 +       {GS_CHROOT_MNT, "chroot_deny_mount", &grsec_enable_chroot_mount, sizeof (int),
11519 +        0600, NULL, &proc_dointvec},
11520 +#endif
11521 +#ifdef CONFIG_GRKERNSEC_CHROOT_FCHDIR
11522 +       {GS_CHROOT_FCHDIR, "chroot_deny_fchdir", &grsec_enable_chroot_fchdir, sizeof (int),
11523 +        0600, NULL, &proc_dointvec},
11524 +#endif
11525 +#ifdef CONFIG_GRKERNSEC_CHROOT_DOUBLE
11526 +       {GS_CHROOT_DBL, "chroot_deny_chroot", &grsec_enable_chroot_double, sizeof (int),
11527 +        0600, NULL, &proc_dointvec},
11528 +#endif
11529 +#ifdef CONFIG_GRKERNSEC_CHROOT_PIVOT
11530 +       {GS_CHROOT_PVT, "chroot_deny_pivot", &grsec_enable_chroot_pivot, sizeof (int),
11531 +        0600, NULL, &proc_dointvec},
11532 +#endif
11533 +#ifdef CONFIG_GRKERNSEC_CHROOT_CHDIR
11534 +       {GS_CHROOT_CD, "chroot_enforce_chdir", &grsec_enable_chroot_chdir, sizeof (int),
11535 +        0600, NULL, &proc_dointvec},
11536 +#endif
11537 +#ifdef CONFIG_GRKERNSEC_CHROOT_CHMOD
11538 +       {GS_CHROOT_CM, "chroot_deny_chmod", &grsec_enable_chroot_chmod, sizeof (int),
11539 +        0600, NULL, &proc_dointvec},
11540 +#endif
11541 +#ifdef CONFIG_GRKERNSEC_CHROOT_MKNOD
11542 +       {GS_CHROOT_MK, "chroot_deny_mknod", &grsec_enable_chroot_mknod, sizeof (int),
11543 +        0600, NULL, &proc_dointvec},
11544 +#endif
11545 +#ifdef CONFIG_GRKERNSEC_CHROOT_NICE
11546 +       {GS_CHROOT_NI, "chroot_restrict_nice", &grsec_enable_chroot_nice, sizeof (int),
11547 +        0600, NULL, &proc_dointvec},
11548 +#endif
11549 +#ifdef CONFIG_GRKERNSEC_CHROOT_EXECLOG
11550 +       {GS_CHROOT_EXECLOG, "chroot_execlog",
11551 +        &grsec_enable_chroot_execlog, sizeof (int),
11552 +         0600, NULL, &proc_dointvec},
11553 +#endif
11554 +#ifdef CONFIG_GRKERNSEC_CHROOT_CAPS
11555 +       {GS_CHROOT_CAPS, "chroot_caps", &grsec_enable_chroot_caps, sizeof (int),
11556 +        0600, NULL, &proc_dointvec},
11557 +#endif
11558 +#ifdef CONFIG_GRKERNSEC_CHROOT_SYSCTL
11559 +       {GS_CHROOT_SYSCTL, "chroot_deny_sysctl", &grsec_enable_chroot_sysctl, sizeof (int),
11560 +        0600, NULL, &proc_dointvec},
11561 +#endif
11562 +#ifdef CONFIG_GRKERNSEC_TPE
11563 +       {GS_TPE, "tpe", &grsec_enable_tpe, sizeof (int),
11564 +        0600, NULL, &proc_dointvec},
11565 +       {GS_TPE_GID, "tpe_gid", &grsec_tpe_gid, sizeof (int),
11566 +        0600, NULL, &proc_dointvec},
11567 +#endif
11568 +#ifdef CONFIG_GRKERNSEC_TPE_ALL
11569 +       {GS_TPE_ALL, "tpe_restrict_all", &grsec_enable_tpe_all, sizeof (int),
11570 +        0600, NULL, &proc_dointvec},
11571 +#endif
11572 +#ifdef CONFIG_GRKERNSEC_RANDPID
11573 +       {GS_RANDPID, "rand_pids", &grsec_enable_randpid, sizeof (int),
11574 +        0600, NULL, &proc_dointvec},
11575 +#endif
11576 +#ifdef CONFIG_GRKERNSEC_RANDID
11577 +       {GS_RANDID, "rand_ip_ids", &grsec_enable_randid, sizeof (int),
11578 +        0600, NULL, &proc_dointvec},
11579 +#endif
11580 +#ifdef CONFIG_GRKERNSEC_RANDSRC
11581 +       {GS_RANDSRC, "rand_tcp_src_ports", &grsec_enable_randsrc, sizeof (int),
11582 +        0600, NULL, &proc_dointvec},
11583 +#endif
11584 +#ifdef CONFIG_GRKERNSEC_RANDISN
11585 +       {GS_RANDISN, "rand_isns", &grsec_enable_randisn, sizeof (int),
11586 +        0600, NULL, &proc_dointvec},
11587 +#endif
11588 +#ifdef CONFIG_GRKERNSEC_SOCKET_ALL
11589 +       {GS_SOCKET_ALL, "socket_all", &grsec_enable_socket_all, sizeof (int),
11590 +        0600, NULL, &proc_dointvec},
11591 +       {GS_SOCKET_ALL_GID, "socket_all_gid",
11592 +        &grsec_socket_all_gid, sizeof (int),
11593 +        0600, NULL, &proc_dointvec},
11594 +#endif
11595 +#ifdef CONFIG_GRKERNSEC_SOCKET_CLIENT
11596 +       {GS_SOCKET_CLIENT, "socket_client", 
11597 +        &grsec_enable_socket_client, sizeof (int),
11598 +        0600, NULL, &proc_dointvec},
11599 +       {GS_SOCKET_CLIENT_GID, "socket_client_gid", 
11600 +        &grsec_socket_client_gid, sizeof (int),
11601 +        0600, NULL, &proc_dointvec},
11602 +#endif
11603 +#ifdef CONFIG_GRKERNSEC_SOCKET_SERVER
11604 +       {GS_SOCKET_SERVER, "socket_server", 
11605 +        &grsec_enable_socket_server, sizeof (int),
11606 +        0600, NULL, &proc_dointvec},
11607 +       {GS_SOCKET_SERVER_GID, "socket_server_gid",
11608 +        &grsec_socket_server_gid, sizeof (int),
11609 +        0600, NULL, &proc_dointvec},
11610 +#endif
11611 +#ifdef CONFIG_GRKERNSEC_AUDIT_GROUP
11612 +       {GS_GROUP, "audit_group", &grsec_enable_group, sizeof (int),
11613 +        0600, NULL, &proc_dointvec},
11614 +       {GS_GID, "audit_gid",
11615 +        &grsec_audit_gid, sizeof (int),
11616 +        0600, NULL, &proc_dointvec},
11617 +#endif
11618 +#ifdef CONFIG_GRKERNSEC_AUDIT_CHDIR
11619 +       {GS_ACHDIR, "audit_chdir", &grsec_enable_chdir, sizeof (int),
11620 +        0600, NULL, &proc_dointvec},
11621 +#endif
11622 +#ifdef CONFIG_GRKERNSEC_AUDIT_MOUNT
11623 +       {GS_AMOUNT, "audit_mount", &grsec_enable_mount, sizeof (int),
11624 +        0600, NULL, &proc_dointvec},
11625 +#endif
11626 +#ifdef CONFIG_GRKERNSEC_AUDIT_IPC
11627 +       {GS_AIPC, "audit_ipc", &grsec_enable_audit_ipc, sizeof (int),
11628 +        0600, NULL, &proc_dointvec},
11629 +#endif
11630 +#ifdef CONFIG_GRKERNSEC_DMESG
11631 +       {GS_AIPC, "dmesg", &grsec_enable_dmesg, sizeof (int),
11632 +        0600, NULL, &proc_dointvec},
11633 +#endif
11634 +#ifdef CONFIG_GRKERNSEC_RANDRPC
11635 +       {GS_RANDRPC, "rand_rpc", &grsec_enable_randrpc, sizeof (int),
11636 +        0600, NULL, &proc_dointvec},
11637 +#endif
11638 +#ifdef CONFIG_GRKERNSEC_CHROOT_FINDTASK
11639 +       {GS_FINDTASK, "chroot_findtask", &grsec_enable_chroot_findtask, 
11640 +        sizeof (int), 0600, NULL, &proc_dointvec},
11641 +#endif
11642 +       {GS_LOCK, "grsec_lock", &grsec_lock, sizeof (int), 0600, NULL,
11643 +        &proc_dointvec},
11644         {0}
11645  };
11646 +#endif
11647  
11648  static ctl_table vm_table[] = {
11649         {VM_BDFLUSH, "bdflush", &bdf_prm, 9*sizeof(int), 0644, NULL,
11650 @@ -395,6 +587,11 @@
11651  
11652  static inline int ctl_perm(ctl_table *table, int op)
11653  {
11654 +       if (gr_handle_sysctl_mod(table->de->parent->name, table->de->name, op))
11655 +               return -EACCES;
11656 +       if (gr_handle_chroot_sysctl(op))
11657 +               return -EACCES;
11658 +
11659         return test_perm(table->mode, op);
11660  }
11661  
11662 @@ -428,6 +625,10 @@
11663                                 table = table->child;
11664                                 goto repeat;
11665                         }
11666 +
11667 +                       if (!gr_handle_sysctl(table, oldval, newval))
11668 +                               return -EACCES;
11669 +
11670                         error = do_sysctl_strategy(table, name, nlen,
11671                                                    oldval, oldlenp,
11672                                                    newval, newlen, context);
11673 diff -urN linux-2.4.21/kernel/time.c linux-2.4.21/kernel/time.c
11674 --- linux-2.4.21/kernel/time.c  2003-06-23 11:41:31.000000000 -0400
11675 +++ linux-2.4.21/kernel/time.c  2003-06-23 11:49:17.000000000 -0400
11676 @@ -27,6 +27,7 @@
11677  #include <linux/mm.h>
11678  #include <linux/timex.h>
11679  #include <linux/smp_lock.h>
11680 +#include <linux/grsecurity.h>
11681  
11682  #include <asm/uaccess.h>
11683  
11684 @@ -89,6 +90,9 @@
11685         time_maxerror = NTP_PHASE_LIMIT;
11686         time_esterror = NTP_PHASE_LIMIT;
11687         write_unlock_irq(&xtime_lock);
11688 +
11689 +       gr_log_timechange();
11690 +
11691         return 0;
11692  }
11693  
11694 @@ -167,6 +171,8 @@
11695                  * globally block out interrupts when it runs.
11696                  */
11697                 do_settimeofday(tv);
11698 +
11699 +               gr_log_timechange();
11700         }
11701         return 0;
11702  }
11703 diff -urN linux-2.4.21/kernel/timer.c linux-2.4.21/kernel/timer.c
11704 --- linux-2.4.21/kernel/timer.c 2003-06-23 11:41:31.000000000 -0400
11705 +++ linux-2.4.21/kernel/timer.c 2003-06-23 11:49:17.000000000 -0400
11706 @@ -541,6 +541,9 @@
11707  
11708         psecs = (p->times.tms_utime += user);
11709         psecs += (p->times.tms_stime += system);
11710 +
11711 +       gr_learn_resource(p, RLIMIT_CPU, psecs / HZ);
11712 +
11713         if (psecs / HZ > p->rlim[RLIMIT_CPU].rlim_cur) {
11714                 /* Send SIGXCPU every second.. */
11715                 if (!(psecs % HZ))
11716 diff -urN linux-2.4.21/mm/filemap.c linux-2.4.21/mm/filemap.c
11717 --- linux-2.4.21/mm/filemap.c   2003-06-23 11:41:31.000000000 -0400
11718 +++ linux-2.4.21/mm/filemap.c   2003-06-23 11:49:17.000000000 -0400
11719 @@ -2501,6 +2507,7 @@
11720         error = -EIO;
11721         rlim_rss = current->rlim ?  current->rlim[RLIMIT_RSS].rlim_cur :
11722                                 LONG_MAX; /* default: see resource.h */
11723 +       gr_learn_resource(current, RLIMIT_RSS, vma->vm_mm->rss + (end - start));
11724         if ((vma->vm_mm->rss + (end - start)) > rlim_rss)
11725                 return error;
11726  
11727 @@ -3004,6 +3011,7 @@
11728         err = -EFBIG;
11729         
11730         if (!S_ISBLK(inode->i_mode) && limit != RLIM_INFINITY) {
11731 +               gr_learn_resource(current, RLIMIT_FSIZE, pos);
11732                 if (pos >= limit) {
11733                         send_sig(SIGXFSZ, current, 0);
11734                         goto out;
11735 @@ -3039,6 +3047,7 @@
11736          */
11737          
11738         if (!S_ISBLK(inode->i_mode)) {
11739 +               gr_learn_resource(current, RLIMIT_FSIZE, count + (u32)pos);
11740                 if (pos >= inode->i_sb->s_maxbytes)
11741                 {
11742                         if (count || pos > inode->i_sb->s_maxbytes) {
11743 diff -urN linux-2.4.21/mm/memory.c linux-2.4.21/mm/memory.c
11744 --- linux-2.4.21/mm/memory.c    2003-06-23 11:41:31.000000000 -0400
11745 +++ linux-2.4.21/mm/memory.c    2003-06-23 11:49:17.000000000 -0400
11746 @@ -1065,6 +1129,7 @@
11747  
11748  do_expand:
11749         limit = current->rlim[RLIMIT_FSIZE].rlim_cur;
11750 +       gr_learn_resource(current, RLIMIT_FSIZE, offset);
11751         if (limit != RLIM_INFINITY && offset > limit)
11752                 goto out_sig;
11753         if (offset > inode->i_sb->s_maxbytes)
11754 diff -urN linux-2.4.21/mm/mlock.c linux-2.4.21/mm/mlock.c
11755 --- linux-2.4.21/mm/mlock.c     2003-06-23 11:41:31.000000000 -0400
11756 +++ linux-2.4.21/mm/mlock.c     2003-06-23 11:49:17.000000000 -0400
11757 @@ -209,6 +235,7 @@
11758         lock_limit >>= PAGE_SHIFT;
11759  
11760         /* check against resource limits */
11761 +       gr_learn_resource(current, RLIMIT_MEMLOCK, locked);
11762         if (locked > lock_limit)
11763                 goto out;
11764  
11765 @@ -276,6 +303,7 @@
11766         lock_limit >>= PAGE_SHIFT;
11767  
11768         ret = -ENOMEM;
11769 +       gr_learn_resource(current, RLIMIT_MEMLOCK, current->mm->total_vm);
11770         if (current->mm->total_vm > lock_limit)
11771                 goto out;
11772  
11773 diff -urN linux-2.4.21/mm/mmap.c linux-2.4.21/mm/mmap.c
11774 --- linux-2.4.21/mm/mmap.c      2003-06-23 11:41:31.000000000 -0400
11775 +++ linux-2.4.21/mm/mmap.c      2003-06-23 11:49:17.000000000 -0400
11776 @@ -14,6 +14,8 @@
11777  #include <linux/file.h>
11778  #include <linux/fs.h>
11779  #include <linux/personality.h>
11780 +#include <linux/random.h>
11781 +#include <linux/grsecurity.h>
11782  
11783  #include <asm/uaccess.h>
11784  #include <asm/pgalloc.h>
11785 @@ -168,6 +170,7 @@
11786  
11787         /* Check against rlimit.. */
11788         rlim = current->rlim[RLIMIT_DATA].rlim_cur;
11789 +       gr_learn_resource(current, RLIMIT_DATA, brk - mm->start_data);
11790         if (rlim < RLIM_INFINITY && brk - mm->start_data > rlim)
11791                 goto out;
11792  
11793 @@ -432,6 +462,7 @@
11794         if (vm_flags & VM_LOCKED) {
11795                 unsigned long locked = mm->locked_vm << PAGE_SHIFT;
11796                 locked += len;
11797 +               gr_learn_resource(current, RLIMIT_MEMLOCK, locked);
11798                 if (locked > current->rlim[RLIMIT_MEMLOCK].rlim_cur)
11799                         return -EAGAIN;
11800         }
11801 @@ -480,6 +532,9 @@
11802                 }
11803         }
11804  
11805 +       if (!gr_acl_handle_mmap(file, prot))
11806 +               return -EACCES;
11807 +
11808         /* Clear old maps */
11809  munmap_back:
11810         vma = find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
11811 @@ -490,6 +545,7 @@
11812         }
11813  
11814         /* Check against address space limit. */
11815 +       gr_learn_resource(current, RLIMIT_AS, (mm->total_vm << PAGE_SHIFT) + len);
11816         if ((mm->total_vm << PAGE_SHIFT) + len
11817             > current->rlim[RLIMIT_AS].rlim_cur)
11818                 return -ENOMEM;
11819 @@ -1047,6 +1255,7 @@
11820         if (mm->def_flags & VM_LOCKED) {
11821                 unsigned long locked = mm->locked_vm << PAGE_SHIFT;
11822                 locked += len;
11823 +               gr_learn_resource(current, RLIMIT_MEMLOCK, locked);
11824                 if (locked > current->rlim[RLIMIT_MEMLOCK].rlim_cur)
11825                         return -EAGAIN;
11826         }
11827 @@ -1063,6 +1272,7 @@
11828         }
11829  
11830         /* Check against address space limits *after* clearing old maps... */
11831 +       gr_learn_resource(current, RLIMIT_AS, (mm->total_vm << PAGE_SHIFT) + len);
11832         if ((mm->total_vm << PAGE_SHIFT) + len
11833             > current->rlim[RLIMIT_AS].rlim_cur)
11834                 return -ENOMEM;
11835 diff -urN linux-2.4.21/mm/mprotect.c linux-2.4.21/mm/mprotect.c
11836 --- linux-2.4.21/mm/mprotect.c  2003-06-23 11:41:31.000000000 -0400
11837 +++ linux-2.4.21/mm/mprotect.c  2003-06-23 11:49:17.000000000 -0400
11838 @@ -7,6 +7,7 @@
11839  #include <linux/smp_lock.h>
11840  #include <linux/shm.h>
11841  #include <linux/mman.h>
11842 +#include <linux/grsecurity.h>
11843  
11844  #include <asm/uaccess.h>
11845  #include <asm/pgalloc.h>
11846 @@ -288,6 +392,11 @@
11847         if (!vma || vma->vm_start > start)
11848                 goto out;
11849  
11850 +       if (!gr_acl_handle_mprotect(vma->vm_file, prot)) {
11851 +               error = -EACCES;
11852 +               goto out;
11853 +       }
11854 +
11855         for (nstart = start ; ; ) {
11856                 unsigned int newflags;
11857                 int last = 0;
11858 diff -urN linux-2.4.21/mm/mremap.c linux-2.4.21/mm/mremap.c
11859 --- linux-2.4.21/mm/mremap.c    2003-06-23 11:41:31.000000000 -0400
11860 +++ linux-2.4.21/mm/mremap.c    2003-06-23 11:49:17.000000000 -0400
11861 @@ -278,10 +301,13 @@
11862                 unsigned long locked = current->mm->locked_vm << PAGE_SHIFT;
11863                 locked += new_len - old_len;
11864                 ret = -EAGAIN;
11865 +               gr_learn_resource(current, RLIMIT_MEMLOCK, locked);
11866                 if (locked > current->rlim[RLIMIT_MEMLOCK].rlim_cur)
11867                         goto out;
11868         }
11869         ret = -ENOMEM;
11870 +
11871 +       gr_learn_resource(current, RLIMIT_AS, (current->mm->total_vm << PAGE_SHIFT) + (new_len - old_len));
11872         if ((current->mm->total_vm << PAGE_SHIFT) + (new_len - old_len)
11873             > current->rlim[RLIMIT_AS].rlim_cur)
11874                 goto out;
11875 diff -urN linux-2.4.21/mm/shmem.c linux-2.4.21/mm/shmem.c
11876 --- linux-2.4.21/mm/shmem.c     2003-06-23 11:41:31.000000000 -0400
11877 +++ linux-2.4.21/mm/shmem.c     2003-06-23 11:49:17.000000000 -0400
11878 @@ -822,6 +822,8 @@
11879          */
11880         err = -EFBIG;
11881         if (limit != RLIM_INFINITY) {
11882 +               gr_learn_resource(current, RLIMIT_FSIZE, pos);
11883 +
11884                 if (pos >= limit) {
11885                         send_sig(SIGXFSZ, current, 0);
11886                         goto out;
11887 diff -urN linux-2.4.21/net/ipv4/af_inet.c linux-2.4.21/net/ipv4/af_inet.c
11888 --- linux-2.4.21/net/ipv4/af_inet.c     2003-06-23 11:41:55.000000000 -0400
11889 +++ linux-2.4.21/net/ipv4/af_inet.c     2003-06-23 11:49:17.000000000 -0400
11890 @@ -83,6 +83,7 @@
11891  #include <linux/init.h>
11892  #include <linux/poll.h>
11893  #include <linux/netfilter_ipv4.h>
11894 +#include <linux/grsecurity.h>
11895  
11896  #include <asm/uaccess.h>
11897  #include <asm/system.h>
11898 @@ -374,7 +375,12 @@
11899         else
11900                 sk->protinfo.af_inet.pmtudisc = IP_PMTUDISC_WANT;
11901  
11902 -       sk->protinfo.af_inet.id = 0;
11903 +#ifdef CONFIG_GRKERNSEC_RANDID
11904 +       if(grsec_enable_randid)
11905 +               sk->protinfo.af_inet.id = htons(ip_randomid());
11906 +       else
11907 +#endif
11908 +               sk->protinfo.af_inet.id = 0;
11909  
11910         sock_init_data(sock,sk);
11911  
11912 diff -urN linux-2.4.21/net/ipv4/icmp.c linux-2.4.21/net/ipv4/icmp.c
11913 --- linux-2.4.21/net/ipv4/icmp.c        2003-06-23 11:41:55.000000000 -0400
11914 +++ linux-2.4.21/net/ipv4/icmp.c        2003-06-23 11:49:17.000000000 -0400
11915 @@ -87,6 +87,8 @@
11916  #include <linux/errno.h>
11917  #include <linux/timer.h>
11918  #include <linux/init.h>
11919 +#include <linux/grsecurity.h>
11920 +
11921  #include <asm/system.h>
11922  #include <asm/uaccess.h>
11923  #include <net/checksum.h>
11924 @@ -712,6 +714,7 @@
11925  
11926                 icmp_param.data.icmph=*skb->h.icmph;
11927                 icmp_param.data.icmph.type=ICMP_ECHOREPLY;
11928 +
11929                 icmp_param.skb=skb;
11930                 icmp_param.offset=0;
11931                 icmp_param.data_len=skb->len;
11932 diff -urN linux-2.4.21/net/ipv4/ip_output.c linux-2.4.21/net/ipv4/ip_output.c
11933 --- linux-2.4.21/net/ipv4/ip_output.c   2003-07-29 17:57:12.000000000 -0400
11934 +++ linux-2.4.21/net/ipv4/ip_output.c   2003-07-29 17:58:20.000000000 -0400
11935 @@ -77,6 +77,7 @@
11936  #include <linux/netfilter_ipv4.h>
11937  #include <linux/mroute.h>
11938  #include <linux/netlink.h>
11939 +#include <linux/grsecurity.h>
11940  
11941  /*
11942   *      Shall we try to damage output packets if routing dev changes?
11943 @@ -511,7 +512,13 @@
11944          *      Begin outputting the bytes.
11945          */
11946  
11947 -       id = sk->protinfo.af_inet.id++;
11948 +#ifdef CONFIG_GRKERNSEC_RANDID
11949 +       if(grsec_enable_randid) { 
11950 +               id = htons(ip_randomid());      
11951 +               sk->protinfo.af_inet.id = htons(ip_randomid());
11952 +       } else
11953 +#endif
11954 +               id = sk->protinfo.af_inet.id++;
11955  
11956         do {
11957                 char *data;
11958 diff -urN linux-2.4.21/net/ipv4/netfilter/Config.in linux-2.4.21/net/ipv4/netfilter/Config.in
11959 --- linux-2.4.21/net/ipv4/netfilter/Config.in   2003-06-23 11:41:55.000000000 -0400
11960 +++ linux-2.4.21/net/ipv4/netfilter/Config.in   2003-06-23 11:49:17.000000000 -0400
11961 @@ -32,6 +32,7 @@
11962    dep_tristate '  address type match support' CONFIG_IP_NF_MATCH_ADDRTYPE $CONFIG_IP_NF_IPTABLES
11963    dep_tristate '  tcpmss match support' CONFIG_IP_NF_MATCH_TCPMSS $CONFIG_IP_NF_IPTABLES
11964    dep_tristate '  realm match support' CONFIG_IP_NF_MATCH_REALM $CONFIG_IP_NF_IPTABLES
11965 +  dep_tristate '  stealth match support' CONFIG_IP_NF_MATCH_STEALTH $CONFIG_IP_NF_IPTABLES
11966    if [ "$CONFIG_IP_NF_CONNTRACK" != "n" ]; then
11967      dep_tristate '  Helper match support' CONFIG_IP_NF_MATCH_HELPER $CONFIG_IP_NF_IPTABLES
11968    fi
11969 diff -urN linux-2.4.21/net/ipv4/netfilter/Makefile linux-2.4.21/net/ipv4/netfilter/Makefile
11970 --- linux-2.4.21/net/ipv4/netfilter/Makefile    2003-06-23 11:41:55.000000000 -0400
11971 +++ linux-2.4.21/net/ipv4/netfilter/Makefile    2003-06-23 11:49:17.000000000 -0400
11972 @@ -83,6 +83,7 @@
11973  obj-$(CONFIG_IP_NF_MATCH_UNCLEAN) += ipt_unclean.o
11974  obj-$(CONFIG_IP_NF_MATCH_STRING) += ipt_string.o
11975  obj-$(CONFIG_IP_NF_MATCH_TCPMSS) += ipt_tcpmss.o
11976 +obj-$(CONFIG_IP_NF_MATCH_STEALTH) += ipt_stealth.o
11977  obj-$(CONFIG_IP_NF_MATCH_ADDRTYPE) += ipt_addrtype.o
11978  obj-$(CONFIG_IP_NF_MATCH_REALM) += ipt_realm.o
11979  
11980 diff -urN linux-2.4.21/net/ipv4/netfilter/ipt_stealth.c linux-2.4.21/net/ipv4/netfilter/ipt_stealth.c
11981 --- linux-2.4.21/net/ipv4/netfilter/ipt_stealth.c       1969-12-31 19:00:00.000000000 -0500
11982 +++ linux-2.4.21/net/ipv4/netfilter/ipt_stealth.c       2003-06-23 11:49:17.000000000 -0400
11983 @@ -0,0 +1,109 @@
11984 +/* Kernel module to add stealth support.
11985 + *
11986 + * Copyright (C) 2002 Brad Spengler  <spender@grsecurity.net>
11987 + *
11988 + */
11989 +
11990 +#include <linux/kernel.h>
11991 +#include <linux/module.h>
11992 +#include <linux/skbuff.h>
11993 +#include <linux/net.h>
11994 +#include <linux/sched.h>
11995 +#include <linux/inet.h>
11996 +#include <linux/stddef.h>
11997 +
11998 +#include <net/ip.h>
11999 +#include <net/sock.h>
12000 +#include <net/tcp.h>
12001 +#include <net/udp.h>
12002 +#include <net/route.h>
12003 +#include <net/inet_common.h>
12004 +
12005 +#include <linux/netfilter_ipv4/ip_tables.h>
12006 +
12007 +MODULE_LICENSE("GPL");
12008 +
12009 +extern struct sock *udp_v4_lookup(u32 saddr, u16 sport, u32 daddr, u16 dport, int dif);
12010 +
12011 +static int
12012 +match(const struct sk_buff *skb,
12013 +      const struct net_device *in,
12014 +      const struct net_device *out,
12015 +      const void *matchinfo,
12016 +      int offset,
12017 +      const void *hdr,
12018 +      u_int16_t datalen,
12019 +      int *hotdrop)
12020 +{
12021 +       struct iphdr *ip = skb->nh.iph;
12022 +       struct tcphdr *th = (struct tcphdr *) hdr;
12023 +       struct udphdr *uh = (struct udphdr *) hdr;
12024 +       struct sock *sk = NULL;
12025 +
12026 +       if (!ip || !hdr || offset) return 0;
12027 +
12028 +       switch(ip->protocol) {
12029 +       case IPPROTO_TCP:
12030 +               if (datalen < sizeof(struct tcphdr)) {
12031 +                       *hotdrop = 1;
12032 +                       return 0;
12033 +               }
12034 +               if (!(th->syn && !th->ack)) return 0;
12035 +               sk = tcp_v4_lookup_listener(ip->daddr, ntohs(th->dest), ((struct rtable*)skb->dst)->rt_iif);    
12036 +               break;
12037 +       case IPPROTO_UDP:
12038 +               if (datalen < sizeof(struct udphdr)) {
12039 +                       *hotdrop = 1;
12040 +                       return 0;
12041 +               }
12042 +               sk = udp_v4_lookup(ip->saddr, uh->source, ip->daddr, uh->dest, skb->dev->ifindex);
12043 +               break;
12044 +       default:
12045 +               return 0;
12046 +       }
12047 +
12048 +       if(!sk) // port is being listened on, match this
12049 +               return 1;
12050 +       else {
12051 +               sock_put(sk);
12052 +               return 0;
12053 +       }
12054 +}
12055 +
12056 +/* Called when user tries to insert an entry of this type. */
12057 +static int
12058 +checkentry(const char *tablename,
12059 +           const struct ipt_ip *ip,
12060 +           void *matchinfo,
12061 +           unsigned int matchsize,
12062 +           unsigned int hook_mask)
12063 +{
12064 +        if (matchsize != IPT_ALIGN(0))
12065 +                return 0;
12066 +
12067 +       if(((ip->proto == IPPROTO_TCP && !(ip->invflags & IPT_INV_PROTO)) ||
12068 +               ((ip->proto == IPPROTO_UDP) && !(ip->invflags & IPT_INV_PROTO)))
12069 +               && (hook_mask & (1 << NF_IP_LOCAL_IN)))
12070 +                       return 1;
12071 +
12072 +       printk("stealth: Only works on TCP and UDP for the INPUT chain.\n");
12073 +
12074 +        return 0;
12075 +}
12076 +
12077 +
12078 +static struct ipt_match stealth_match
12079 += { { NULL, NULL }, "stealth", &match, &checkentry, NULL, THIS_MODULE };
12080 +
12081 +static int __init init(void)
12082 +{
12083 +       return ipt_register_match(&stealth_match);
12084 +}
12085 +
12086 +static void __exit fini(void)
12087 +{
12088 +       ipt_unregister_match(&stealth_match);
12089 +}
12090 +
12091 +module_init(init);
12092 +module_exit(fini);
12093 diff -urN linux-2.4.21/net/ipv4/tcp_ipv4.c linux-2.4.21/net/ipv4/tcp_ipv4.c
12094 --- linux-2.4.21/net/ipv4/tcp_ipv4.c    2003-06-23 11:41:55.000000000 -0400
12095 +++ linux-2.4.21/net/ipv4/tcp_ipv4.c    2003-06-23 11:49:17.000000000 -0400
12096 @@ -67,6 +67,7 @@
12097  #include <linux/inet.h>
12098  #include <linux/stddef.h>
12099  #include <linux/ipsec.h>
12100 +#include <linux/grsecurity.h>
12101  
12102  extern int sysctl_ip_dynaddr;
12103  extern int sysctl_ip_default_ttl;
12104 @@ -221,9 +222,18 @@
12105  
12106                 spin_lock(&tcp_portalloc_lock);
12107                 rover = tcp_port_rover;
12108 -               do {    rover++;
12109 -                       if ((rover < low) || (rover > high))
12110 -                               rover = low;
12111 +                do {
12112 +#ifdef CONFIG_GRKERNSEC_RANDSRC
12113 +                       if (grsec_enable_randsrc && (high > low)) {
12114 +                               rover = low + (arc4random() % (high - low));
12115 +                       } else
12116 +#endif
12117 +                       {
12118 +                               rover++;
12119 +                               if ((rover < low) || (rover > high))
12120 +                                       rover = low;
12121 +                       }
12122 +
12123                         head = &tcp_bhash[tcp_bhashfn(rover)];
12124                         spin_lock(&head->lock);
12125                         for (tb = head->chain; tb; tb = tb->next)
12126 @@ -546,6 +556,11 @@
12127  
12128  static inline __u32 tcp_v4_init_sequence(struct sock *sk, struct sk_buff *skb)
12129  {
12130 +#ifdef CONFIG_GRKERNSEC_RANDISN
12131 +       if (likely(grsec_enable_randisn))
12132 +               return ip_randomisn();
12133 +       else
12134 +#endif
12135         return secure_tcp_sequence_number(skb->nh.iph->daddr,
12136                                           skb->nh.iph->saddr,
12137                                           skb->h.th->dest,
12138 @@ -681,9 +696,16 @@
12139                 rover = tcp_port_rover;
12140  
12141                 do {
12142 -                       rover++;
12143 -                       if ((rover < low) || (rover > high))
12144 -                               rover = low;
12145 +#ifdef CONFIG_GRKERNSEC_RANDSRC
12146 +                       if(grsec_enable_randsrc && (high > low)) {
12147 +                               rover = low + (arc4random() % (high - low));
12148 +                       } else
12149 +#endif
12150 +                       {
12151 +                               rover++;
12152 +                               if ((rover < low) || (rover > high))
12153 +                                       rover = low;
12154 +                       }
12155                         head = &tcp_bhash[tcp_bhashfn(rover)];
12156                         spin_lock(&head->lock);         
12157  
12158 @@ -844,11 +866,22 @@
12159         if (err)
12160                 goto failure;
12161  
12162 -       if (!tp->write_seq)
12163 +       if (!tp->write_seq) {
12164 +#ifdef CONFIG_GRKERNSEC_RANDISN
12165 +               if (likely(grsec_enable_randisn))
12166 +                       tp->write_seq = ip_randomisn();
12167 +               else
12168 +#endif
12169                 tp->write_seq = secure_tcp_sequence_number(sk->saddr, sk->daddr,
12170                                                            sk->sport, usin->sin_port);
12171 +       }
12172  
12173 -       sk->protinfo.af_inet.id = tp->write_seq^jiffies;
12174 +#ifdef CONFIG_GRKERNSEC_RANDID
12175 +       if(grsec_enable_randid)
12176 +               sk->protinfo.af_inet.id = htons(ip_randomid());
12177 +       else
12178 +#endif
12179 +               sk->protinfo.af_inet.id = tp->write_seq^jiffies;
12180  
12181         err = tcp_connect(sk);
12182         if (err)
12183 @@ -1570,7 +1603,13 @@
12184         newtp->ext_header_len = 0;
12185         if (newsk->protinfo.af_inet.opt)
12186                 newtp->ext_header_len = newsk->protinfo.af_inet.opt->optlen;
12187 -       newsk->protinfo.af_inet.id = newtp->write_seq^jiffies;
12188 +
12189 +#ifdef CONFIG_GRKERNSEC_RANDID
12190 +       if(grsec_enable_randid)
12191 +               newsk->protinfo.af_inet.id = htons(ip_randomid());
12192 +       else
12193 +#endif
12194 +               newsk->protinfo.af_inet.id = newtp->write_seq^jiffies;
12195  
12196         tcp_sync_mss(newsk, dst->pmtu);
12197         newtp->advmss = dst->advmss;
12198 diff -urN linux-2.4.21/net/ipv4/udp.c linux-2.4.21/net/ipv4/udp.c
12199 --- linux-2.4.21/net/ipv4/udp.c 2003-06-23 11:41:55.000000000 -0400
12200 +++ linux-2.4.21/net/ipv4/udp.c 2003-06-23 11:49:17.000000000 -0400
12201 @@ -91,6 +91,7 @@
12202  #include <net/ipv6.h>
12203  #include <net/protocol.h>
12204  #include <linux/skbuff.h>
12205 +#include <linux/grsecurity.h>
12206  #include <net/sock.h>
12207  #include <net/udp.h>
12208  #include <net/icmp.h>
12209 @@ -98,6 +99,11 @@
12210  #include <net/inet_common.h>
12211  #include <net/checksum.h>
12212  
12213 +extern int gr_search_udp_recvmsg(const struct sock *sk,
12214 +                                       const struct sk_buff *skb);
12215 +extern int gr_search_udp_sendmsg(const struct sock *sk,
12216 +                                       const struct sockaddr_in *addr);
12217 +
12218  /*
12219   *     Snmp MIB for the UDP layer
12220   */
12221 @@ -474,9 +480,16 @@
12222                 ufh.uh.dest = usin->sin_port;
12223                 if (ufh.uh.dest == 0)
12224                         return -EINVAL;
12225 +
12226 +               if (!gr_search_udp_sendmsg(sk, usin))
12227 +                       return -EPERM;
12228         } else {
12229                 if (sk->state != TCP_ESTABLISHED)
12230                         return -ENOTCONN;
12231 +
12232 +               if (!gr_search_udp_sendmsg(sk, NULL))
12233 +                       return -EPERM;
12234 +
12235                 ufh.daddr = sk->daddr;
12236                 ufh.uh.dest = sk->dport;
12237                 /* Open fast path for connected socket.
12238 @@ -484,6 +497,7 @@
12239                  */
12240                 connected = 1;
12241         }
12242 +
12243         ipc.addr = sk->saddr;
12244         ufh.uh.source = sk->sport;
12245  
12246 @@ -655,6 +669,11 @@
12247         if (!skb)
12248                 goto out;
12249    
12250 +       if (!gr_search_udp_recvmsg(sk, skb)) {
12251 +               err = -EPERM;
12252 +               goto out_free;
12253 +       }
12254 +
12255         copied = skb->len - sizeof(struct udphdr);
12256         if (copied > len) {
12257                 copied = len;
12258 @@ -759,7 +778,13 @@
12259         sk->daddr = rt->rt_dst;
12260         sk->dport = usin->sin_port;
12261         sk->state = TCP_ESTABLISHED;
12262 -       sk->protinfo.af_inet.id = jiffies;
12263 +
12264 +#ifdef CONFIG_GRKERNSEC_RANDID
12265 +       if(grsec_enable_randid)
12266 +               sk->protinfo.af_inet.id = htons(ip_randomid());
12267 +       else
12268 +#endif
12269 +               sk->protinfo.af_inet.id = jiffies;
12270  
12271         sk_dst_set(sk, &rt->u.dst);
12272         return(0);
12273 diff -urN linux-2.4.21/net/netlink/af_netlink.c linux-2.4.21/net/netlink/af_netlink.c
12274 --- linux-2.4.21/net/netlink/af_netlink.c       2003-06-23 11:41:58.000000000 -0400
12275 +++ linux-2.4.21/net/netlink/af_netlink.c       2003-06-23 11:49:17.000000000 -0400
12276 @@ -40,6 +40,7 @@
12277  #include <linux/proc_fs.h>
12278  #include <linux/smp_lock.h>
12279  #include <linux/notifier.h>
12280 +#include <linux/grsecurity.h>
12281  #include <net/sock.h>
12282  #include <net/scm.h>
12283  
12284 @@ -620,7 +621,8 @@
12285            check them, when this message will be delivered
12286            to corresponding kernel module.   --ANK (980802)
12287          */
12288 -       NETLINK_CB(skb).eff_cap = current->cap_effective;
12289 +
12290 +       NETLINK_CB(skb).eff_cap = gr_cap_rtnetlink();
12291  
12292         err = -EFAULT;
12293         if (memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len)) {
12294 diff -urN linux-2.4.21/net/netsyms.c linux-2.4.21/net/netsyms.c
12295 --- linux-2.4.21/net/netsyms.c  2003-06-23 11:41:53.000000000 -0400
12296 +++ linux-2.4.21/net/netsyms.c  2003-06-23 11:49:17.000000000 -0400
12297 @@ -24,6 +24,7 @@
12298  #include <net/checksum.h>
12299  #include <linux/etherdevice.h>
12300  #include <net/route.h>
12301 +#include <linux/grsecurity.h>
12302  #ifdef CONFIG_HIPPI
12303  #include <linux/hippidevice.h>
12304  #endif
12305 @@ -598,6 +599,47 @@
12306  
12307  EXPORT_SYMBOL(softnet_data);
12308  
12309 +#if defined(CONFIG_IP_NF_MATCH_STEALTH_MODULE)
12310 +#if !defined (CONFIG_IPV6_MODULE) && !defined (CONFIG_KHTTPD) && !defined (CONFIG_KHTTPD_MODULE)
12311 +EXPORT_SYMBOL(tcp_v4_lookup_listener);
12312 +#endif
12313 +#endif
12314 +
12315 +#if defined(CONFIG_GRKERNSEC_RANDID)
12316 +EXPORT_SYMBOL(ip_randomid);
12317 +#endif
12318 +#if defined(CONFIG_GRKERNSEC_RANDSRC) || defined(CONFIG_GRKERNSEC_RANDRPC)
12319 +EXPORT_SYMBOL(arc4random);
12320 +#endif
12321 +#ifdef CONFIG_GRKERNSEC_RANDISN
12322 +EXPORT_SYMBOL(ip_randomisn);
12323 +EXPORT_SYMBOL(grsec_enable_randisn);
12324 +#endif
12325 +#ifdef CONFIG_GRKERNSEC_RANDID
12326 +EXPORT_SYMBOL(grsec_enable_randid);
12327 +#endif
12328 +#ifdef CONFIG_GRKERNSEC_RANDSRC
12329 +EXPORT_SYMBOL(grsec_enable_randsrc);
12330 +#endif
12331 +#ifdef CONFIG_GRKERNSEC_RANDRPC
12332 +EXPORT_SYMBOL(grsec_enable_randrpc);
12333 +#endif
12334 +
12335 +EXPORT_SYMBOL(gr_cap_rtnetlink);
12336 +
12337 +extern int gr_search_udp_recvmsg(const struct sock *sk, const struct sk_buff *skb);
12338 +extern int gr_search_udp_sendmsg(const struct sock *sk, const struct sockaddr_in *addr);
12339 +
12340 +EXPORT_SYMBOL(gr_search_udp_recvmsg);
12341 +EXPORT_SYMBOL(gr_search_udp_sendmsg);
12342 +
12343 +#ifdef CONFIG_UNIX_MODULE
12344 +EXPORT_SYMBOL(gr_acl_handle_unix);
12345 +EXPORT_SYMBOL(gr_acl_handle_mknod);
12346 +EXPORT_SYMBOL(gr_handle_chroot_unix);
12347 +EXPORT_SYMBOL(gr_handle_create);
12348 +#endif
12349 +
12350  #if defined(CONFIG_NET_RADIO) || defined(CONFIG_NET_PCMCIA_RADIO)
12351  #include <net/iw_handler.h>
12352  EXPORT_SYMBOL(wireless_send_event);
12353 diff -urN linux-2.4.21/net/socket.c linux-2.4.21/net/socket.c
12354 --- linux-2.4.21/net/socket.c   2003-06-23 11:41:53.000000000 -0400
12355 +++ linux-2.4.21/net/socket.c   2003-06-23 11:49:17.000000000 -0400
12356 @@ -85,6 +85,18 @@
12357  #include <net/scm.h>
12358  #include <linux/netfilter.h>
12359  
12360 +extern void gr_attach_curr_ip(const struct sock *sk);
12361 +extern int gr_handle_sock_all(const int family, const int type,
12362 +                             const int protocol);
12363 +extern int gr_handle_sock_server(const struct sockaddr *sck);  
12364 +extern int gr_handle_sock_client(const struct sockaddr *sck);
12365 +extern int gr_search_connect(const struct socket * sock,
12366 +                            const struct sockaddr_in * addr);  
12367 +extern int gr_search_bind(const struct socket * sock,
12368 +                         const struct sockaddr_in * addr);
12369 +extern int gr_search_socket(const int domain, const int type,
12370 +                           const int protocol);
12371 +
12372  static int sock_no_open(struct inode *irrelevant, struct file *dontcare);
12373  static ssize_t sock_read(struct file *file, char *buf,
12374                          size_t size, loff_t *ppos);
12375 @@ -699,6 +711,7 @@
12376  
12377  int sock_close(struct inode *inode, struct file *filp)
12378  {
12379 +       struct socket *sock;
12380         /*
12381          *      It was possible the inode is NULL we were 
12382          *      closing an unfinished socket. 
12383 @@ -709,8 +722,21 @@
12384                 printk(KERN_DEBUG "sock_close: NULL inode\n");
12385                 return 0;
12386         }
12387 +       sock = socki_lookup(inode);
12388 +
12389         sock_fasync(-1, filp, 0);
12390 +
12391 +#ifdef CONFIG_GRKERNSEC
12392 +       if (unlikely(current->used_accept && sock->sk &&
12393 +                    (sock->sk->protocol == IPPROTO_TCP) &&
12394 +                    (sock->sk->daddr == current->curr_ip))) {
12395 +               current->used_accept = 0;
12396 +               current->curr_ip = 0;
12397 +       }
12398 +#endif
12399 +
12400         sock_release(socki_lookup(inode));
12401 +
12402         return 0;
12403  }
12404  
12405 @@ -903,6 +929,16 @@
12406         int retval;
12407         struct socket *sock;
12408  
12409 +       if(!gr_search_socket(family, type, protocol)) {
12410 +               retval = -EACCES;
12411 +               goto out;
12412 +       }
12413 +
12414 +       if (gr_handle_sock_all(family, type, protocol)) {
12415 +               retval = -EACCES;
12416 +               goto out;
12417 +       }
12418 +
12419         retval = sock_create(family, type, protocol, &sock);
12420         if (retval < 0)
12421                 goto out;
12422 @@ -998,12 +1034,26 @@
12423  {
12424         struct socket *sock;
12425         char address[MAX_SOCK_ADDR];
12426 +       struct sockaddr * sck;
12427         int err;
12428  
12429         if((sock = sockfd_lookup(fd,&err))!=NULL)
12430         {
12431 -               if((err=move_addr_to_kernel(umyaddr,addrlen,address))>=0)
12432 +               if((err=move_addr_to_kernel(umyaddr,addrlen,address))>=0) {
12433 +                       sck = (struct sockaddr *) address;
12434 +
12435 +                       if(!gr_search_bind(sock, (struct sockaddr_in *) sck)) {
12436 +                               sockfd_put(sock);
12437 +                               return -EACCES;
12438 +                       }
12439 +
12440 +                       if (gr_handle_sock_server(sck)) {
12441 +                               sockfd_put(sock);
12442 +                               return -EACCES;
12443 +                       }
12444 +
12445                         err = sock->ops->bind(sock, (struct sockaddr *)address, addrlen);
12446 +               }
12447                 sockfd_put(sock);
12448         }                       
12449         return err;
12450 @@ -1079,6 +1129,8 @@
12451         if ((err = sock_map_fd(newsock)) < 0)
12452                 goto out_release;
12453  
12454 +       gr_attach_curr_ip(newsock->sk);
12455 +
12456  out_put:
12457         sockfd_put(sock);
12458  out:
12459 @@ -1106,6 +1158,7 @@
12460  {
12461         struct socket *sock;
12462         char address[MAX_SOCK_ADDR];
12463 +       struct sockaddr * sck;
12464         int err;
12465  
12466         sock = sockfd_lookup(fd, &err);
12467 @@ -1114,6 +1167,24 @@
12468         err = move_addr_to_kernel(uservaddr, addrlen, address);
12469         if (err < 0)
12470                 goto out_put;
12471 +
12472 +       sck = (struct sockaddr *) address;
12473 +
12474 +       if (!gr_search_connect(sock, (struct sockaddr_in *) sck)) {
12475 +               err = -EACCES;
12476 +               goto out_put;
12477 +       }
12478 +
12479 +       if (gr_handle_sock_client(sck)) {
12480 +               err = -EACCES;
12481 +               goto out_put;
12482 +       }
12483 +
12484 +#ifdef CONFIG_GRKERNSEC
12485 +       if (sock->sk->protocol == IPPROTO_TCP)
12486 +               current->used_connect = 1;
12487 +#endif
12488 +
12489         err = sock->ops->connect(sock, (struct sockaddr *) address, addrlen,
12490                                  sock->file->f_flags);
12491  out_put:
12492 @@ -1333,6 +1404,14 @@
12493                 err=sock->ops->shutdown(sock, how);
12494                 sockfd_put(sock);
12495         }
12496 +
12497 +#ifdef CONFIG_GRKERNSEC
12498 +       if (likely(!err && current->used_accept)) {
12499 +               current->used_accept = 0;
12500 +               current->curr_ip = 0;
12501 +       }
12502 +#endif
12503 +
12504         return err;
12505  }
12506  
12507 diff -urN linux-2.4.21/net/sunrpc/xprt.c linux-2.4.21/net/sunrpc/xprt.c
12508 --- linux-2.4.21/net/sunrpc/xprt.c      2003-06-23 11:41:57.000000000 -0400
12509 +++ linux-2.4.21/net/sunrpc/xprt.c      2003-06-23 11:49:17.000000000 -0400
12510 @@ -59,6 +59,7 @@
12511  #include <linux/unistd.h>
12512  #include <linux/sunrpc/clnt.h>
12513  #include <linux/file.h>
12514 +#include <linux/grsecurity.h>
12515  
12516  #include <net/sock.h>
12517  #include <net/checksum.h>
12518 @@ -1267,6 +1268,12 @@
12519         }
12520         ret = xid++;
12521         spin_unlock(&xid_lock);
12522 +
12523 +#ifdef CONFIG_GRKERNSEC_RANDRPC
12524 +       if (grsec_enable_randrpc)
12525 +               ret = arc4random();
12526 +#endif
12527 +
12528         return ret;
12529  }
12530  
12531 diff -urN linux-2.4.21/net/unix/af_unix.c linux-2.4.21/net/unix/af_unix.c
12532 --- linux-2.4.21/net/unix/af_unix.c     2003-06-23 11:41:53.000000000 -0400
12533 +++ linux-2.4.21/net/unix/af_unix.c     2003-06-23 11:49:18.000000000 -0400
12534 @@ -109,6 +109,7 @@
12535  #include <linux/poll.h>
12536  #include <linux/smp_lock.h>
12537  #include <linux/rtnetlink.h>
12538 +#include <linux/grsecurity.h>
12539  
12540  #include <asm/checksum.h>
12541  
12542 @@ -599,6 +600,11 @@
12543                 if (err)
12544                         goto put_fail;
12545  
12546 +               if (!gr_acl_handle_unix(nd.dentry, nd.mnt)) {
12547 +                       err = -EACCES;
12548 +                       goto put_fail;
12549 +               }
12550 +               
12551                 err = -ECONNREFUSED;
12552                 if (!S_ISSOCK(nd.dentry->d_inode->i_mode))
12553                         goto put_fail;
12554 @@ -622,6 +628,13 @@
12555                 if (u) {
12556                         struct dentry *dentry;
12557                         dentry = u->protinfo.af_unix.dentry;
12558 +
12559 +                       if (!gr_handle_chroot_unix(u->peercred.pid)) {
12560 +                               err = -EPERM;
12561 +                               sock_put(u);
12562 +                               goto fail;
12563 +                       }
12564 +
12565                         if (dentry)
12566                                 UPDATE_ATIME(dentry->d_inode);
12567                 } else
12568 @@ -720,9 +733,19 @@
12569                  * All right, let's create it.
12570                  */
12571                 mode = S_IFSOCK | (sock->inode->i_mode & ~current->fs->umask);
12572 +       
12573 +               if (!gr_acl_handle_mknod(dentry, nd.dentry, nd.mnt, mode)) {
12574 +                       err = -EACCES;
12575 +                       goto out_mknod_dput;
12576 +               }       
12577 +
12578                 err = vfs_mknod(nd.dentry->d_inode, dentry, mode, 0);
12579 +
12580                 if (err)
12581                         goto out_mknod_dput;
12582 +
12583 +               gr_handle_create(dentry, nd.mnt);
12584 +
12585                 up(&nd.dentry->d_inode->i_sem);
12586                 dput(nd.dentry);
12587                 nd.dentry = dentry;
12588 @@ -740,6 +763,10 @@
12589                         goto out_unlock;
12590                 }
12591  
12592 +#ifdef CONFIG_GRKERNSEC_CHROOT_UNIX
12593 +               sk->peercred.pid = current->pid;
12594 +#endif
12595 +
12596                 list = &unix_socket_table[addr->hash];
12597         } else {
12598                 list = &unix_socket_table[dentry->d_inode->i_ino & (UNIX_HASH_SIZE-1)];
12599 @@ -866,6 +893,9 @@
12600         int st;
12601         int err;
12602         long timeo;
12603 +#ifdef CONFIG_GRKERNSEC
12604 +       struct task_struct *p, **htable;
12605 +#endif
12606  
12607         err = unix_mkname(sunaddr, addr_len, &hash);
12608         if (err < 0)
12609 @@ -989,6 +1019,17 @@
12610         /* Set credentials */
12611         sk->peercred = other->peercred;
12612  
12613 +#ifdef CONFIG_GRKERNSEC
12614 +       read_lock(&tasklist_lock);
12615 +       htable = &pidhash[pid_hashfn(other->peercred.pid)];
12616 +       for (p = *htable; p && p->pid != other->peercred.pid; p = p->pidhash_next);
12617 +       if (p) {
12618 +               p->curr_ip = current->curr_ip;
12619 +               p->used_accept = 1;
12620 +       }
12621 +       read_unlock(&tasklist_lock);
12622 +#endif
12623 +
12624         sock_hold(newsk);
12625         unix_peer(sk)=newsk;
12626         sock->state=SS_CONNECTED;
This page took 1.104381 seconds and 3 git commands to generate.