]> git.pld-linux.org Git - packages/kernel.git/blame - grsecurity-nopax-2.0-rc1-2.4.21.patch
- [2.4.2x, 2.6.x] don't recursively crash in die() on CHRP/PReP machines
[packages/kernel.git] / grsecurity-nopax-2.0-rc1-2.4.21.patch
CommitLineData
dd08e044
JR
1diff -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,619 @@
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+Altered Ping IDs
525+CONFIG_GRKERNSEC_RANDPING
526+ If you say Y here, the way Linux handles echo replies will be changed
527+ so that the reply uses an ID equal to the ID of the echo request.
528+ This will help in confusing OS detection. If the sysctl option is
529+ enabled, a sysctl option with name "altered_pings" is created.
530+
531+Socket restrictions
532+CONFIG_GRKERNSEC_SOCKET
533+ If you say Y here, you will be able to choose from several options.
534+ If you assign a GID on your system and add it to the supplementary
535+ groups of users you want to restrict socket access to, this patch
536+ will perform up to three things, based on the option(s) you choose.
537+
538+Deny all socket access
539+CONFIG_GRKERNSEC_SOCKET_ALL
540+ If you say Y here, you will be able to choose a GID of whose users will
541+ be unable to connect to other hosts from your machine or run server
542+ applications from your machine. If the sysctl option is enabled, a
543+ sysctl option with name "socket_all" is created.
544+
545+Group for disabled socket access
546+CONFIG_GRKERNSEC_SOCKET_ALL_GID
547+ Here you can choose the GID to disable socket access for. Remember to
548+ add the users you want socket access disabled for to the GID
549+ specified here. If the sysctl option is enabled, whatever you choose
550+ here won't matter. You'll have to specify the GID in your bootup
551+ script by echoing the GID to the proper /proc entry. View the help
552+ on the sysctl option for more information. If the sysctl option is
553+ enabled, a sysctl option with name "socket_all_gid" is created.
554+
555+Deny all client socket access
556+CONFIG_GRKERNSEC_SOCKET_CLIENT
557+ If you say Y here, you will be able to choose a GID of whose users will
558+ be unable to connect to other hosts from your machine, but will be
559+ able to run servers. If this option is enabled, all users in the group
560+ you specify will have to use passive mode when initiating ftp transfers
561+ from the shell on your machine. If the sysctl option is enabled, a
562+ sysctl option with name "socket_client" is created.
563+
564+Group for disabled client socket access
565+CONFIG_GRKERNSEC_SOCKET_CLIENT_GID
566+ Here you can choose the GID to disable client socket access for.
567+ Remember to add the users you want client socket access disabled for to
568+ the GID specified here. If the sysctl option is enabled, whatever you
569+ choose here won't matter. You'll have to specify the GID in your bootup
570+ script by echoing the GID to the proper /proc entry. View the help
571+ on the sysctl option for more information. If the sysctl option is
572+ enabled, a sysctl option with name "socket_client_gid" is created.
573+
574+Deny all server socket access
575+CONFIG_GRKERNSEC_SOCKET_SERVER
576+ If you say Y here, you will be able to choose a GID of whose users will
577+ be unable to run server applications from your machine. If the sysctl
578+ option is enabled, a sysctl option with name "socket_server" is created.
579+
580+Group for disabled server socket access
581+CONFIG_GRKERNSEC_SOCKET_SERVER_GID
582+ Here you can choose the GID to disable server socket access for.
583+ Remember to add the users you want server socket access disabled for to
584+ the GID specified here. If the sysctl option is enabled, whatever you
585+ choose here won't matter. You'll have to specify the GID in your bootup
586+ script by echoing the GID to the proper /proc entry. View the help
587+ on the sysctl option for more information. If the sysctl option is
588+ enabled, a sysctl option with name "socket_server_gid" is created.
589+
590+Sysctl support
591+CONFIG_GRKERNSEC_SYSCTL
592+ If you say Y here, you will be able to change the options that
593+ grsecurity runs with at bootup, without having to recompile your
594+ kernel. You can echo values to files in /proc/sys/kernel/grsecurity
595+ to enable (1) or disable (0) various features. All the sysctl entries
596+ are mutable until the "grsec_lock" entry is set to a non-zero value.
597+ All features are disabled by default. Please note that this option could
598+ reduce the effectiveness of the added security of this patch if an ACL
599+ system is not put in place. Your init scripts should be read-only, and
600+ root should not have access to adding modules or performing raw i/o
601+ operations. All options should be set at startup, and the grsec_lock
602+ entry should be set to a non-zero value after all the options are set.
603+ *THIS IS EXTREMELY IMPORTANT*
604+
605+Number of burst messages
606+CONFIG_GRKERNSEC_FLOODBURST
607+ This option allows you to choose the maximum number of messages allowed
608+ within the flood time interval you chose in a separate option. The
609+ default should be suitable for most people, however if you find that
610+ many of your logs are being interpreted as flooding, you may want to
611+ raise this value.
612+
613+Seconds in between log messages
614+CONFIG_GRKERNSEC_FLOODTIME
615+ This option allows you to enforce the number of seconds between
616+ grsecurity log messages. The default should be suitable for most
617+ people, however, if you choose to change it, choose a value small enough
618+ to allow informative logs to be produced, but large enough to
619+ prevent flooding.
620+
621+Hide kernel processes
622+CONFIG_GRKERNSEC_ACL_HIDEKERN
623+ If you say Y here, when the ACL system is enabled via gradm -E,
624+ an additional ACL will be passed to the kernel that hides all kernel
625+ processes. These processes will only be viewable by the authenticated
626+ admin, or processes that have viewing access set.
627+
628+Maximum tries before password lockout
629+CONFIG_GRKERNSEC_ACL_MAXTRIES
630+ This option enforces the maximum number of times a user can attempt
631+ to authorize themselves with the grsecurity ACL system before being
632+ denied the ability to attempt authorization again for a specified time.
633+ The lower the number, the harder it will be to brute-force a password.
634+
635+Time to wait after max password tries, in seconds
636+CONFIG_GRKERNSEC_ACL_TIMEOUT
637+ This option specifies the time the user must wait after attempting to
638+ authorize to the ACL system with the maximum number of invalid
639+ passwords. The higher the number, the harder it will be to brute-force
640+ a password.
641+
642 Disable data cache
643 CONFIG_DCACHE_DISABLE
644 This option allows you to run the kernel with data cache disabled.
645diff -urN linux-2.4.21/Makefile linux-2.4.21/Makefile
646--- linux-2.4.21/Makefile 2003-06-23 11:41:28.000000000 -0400
647+++ linux-2.4.21/Makefile 2003-06-23 11:49:15.000000000 -0400
648@@ -123,9 +123,10 @@
649 CORE_FILES =kernel/kernel.o mm/mm.o fs/fs.o ipc/ipc.o
650 NETWORKS =net/network.o
651 CRYPTO =crypto/crypto.o
652+GRSECURITY =grsecurity/grsec.o
653
654 LIBS =$(TOPDIR)/lib/lib.a
655-SUBDIRS =kernel drivers mm fs net ipc lib crypto
656+SUBDIRS =kernel drivers mm fs net ipc lib crypto grsecurity
657
658 DRIVERS-n :=
659 DRIVERS-y :=
660@@ -265,7 +266,7 @@
661
662 export CPPFLAGS CFLAGS CFLAGS_KERNEL AFLAGS AFLAGS_KERNEL
663
664-export NETWORKS CRYPTO DRIVERS LIBS HEAD LDFLAGS LINKFLAGS MAKEBOOT ASFLAGS
665+export NETWORKS CRYPTO DRIVERS LIBS HEAD LDFLAGS LINKFLAGS MAKEBOOT ASFLAGS GRSECURITY
666
667 .S.s:
668 $(CPP) $(AFLAGS) $(AFLAGS_KERNEL) -traditional -o $*.s $<
669@@ -284,6 +285,7 @@
670 $(CORE_FILES) \
671 $(DRIVERS) \
672 $(NETWORKS) \
673 $(CRYPTO) \
674+ $(GRSECURITY) \
675 $(LIBS) \
676 --end-group \
677 -o vmlinux
678diff -urN linux-2.4.21/arch/alpha/config.in linux-2.4.21/arch/alpha/config.in
679--- linux-2.4.21/arch/alpha/config.in 2003-06-23 11:42:24.000000000 -0400
680+++ linux-2.4.21/arch/alpha/config.in 2003-06-23 11:49:15.000000000 -0400
681@@ -444,3 +444,12 @@
682 endmenu
683
684 source lib/Config.in
685+
686+mainmenu_option next_comment
687+comment 'Grsecurity'
688+bool 'Grsecurity' CONFIG_GRKERNSEC
689+if [ "$CONFIG_GRKERNSEC" = "y" ]; then
690+ source grsecurity/Config.in
691+fi
692+endmenu
693+
694diff -urN linux-2.4.21/arch/alpha/kernel/osf_sys.c linux-2.4.21/arch/alpha/kernel/osf_sys.c
695--- linux-2.4.21/arch/alpha/kernel/osf_sys.c 2003-06-23 11:42:24.000000000 -0400
696+++ linux-2.4.21/arch/alpha/kernel/osf_sys.c 2003-06-23 11:49:15.000000000 -0400
697@@ -33,6 +33,7 @@
698 #include <linux/file.h>
699 #include <linux/types.h>
700 #include <linux/ipc.h>
701+#include <linux/grsecurity.h>
702
703 #include <asm/fpu.h>
704 #include <asm/io.h>
705@@ -240,6 +246,13 @@
706 if (!file)
707 goto out;
708 }
709+
710+ if(gr_handle_mmap(file, prot)) {
711+ fput(file);
712+ ret = -EACCES;
713+ goto out;
714+ }
715+
716 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
717 down_write(&current->mm->mmap_sem);
718 ret = do_mmap(file, addr, len, prot, flags, off);
719diff -urN linux-2.4.21/arch/alpha/kernel/ptrace.c linux-2.4.21/arch/alpha/kernel/ptrace.c
720--- linux-2.4.21/arch/alpha/kernel/ptrace.c 2003-06-23 11:42:24.000000000 -0400
721+++ linux-2.4.21/arch/alpha/kernel/ptrace.c 2003-06-23 11:49:15.000000000 -0400
722@@ -13,6 +13,7 @@
723 #include <linux/ptrace.h>
724 #include <linux/user.h>
725 #include <linux/slab.h>
726+#include <linux/grsecurity.h>
727
728 #include <asm/uaccess.h>
729 #include <asm/pgtable.h>
730@@ -275,6 +276,10 @@
731 read_unlock(&tasklist_lock);
732 if (!child)
733 goto out_notsk;
734+
735+ if(gr_handle_ptrace(child, request))
736+ goto out;
737+
738 if (request == PTRACE_ATTACH) {
739 ret = ptrace_attach(child);
740 goto out;
741diff -urN linux-2.4.21/arch/arm/config.in linux-2.4.21/arch/arm/config.in
742--- linux-2.4.21/arch/arm/config.in 2003-06-23 11:42:30.000000000 -0400
743+++ linux-2.4.21/arch/arm/config.in 2003-06-23 11:49:15.000000000 -0400
744@@ -657,3 +657,11 @@
745 endmenu
746
747 source lib/Config.in
748+
749+mainmenu_option next_comment
750+comment 'Grsecurity'
751+bool 'Grsecurity' CONFIG_GRKERNSEC
752+if [ "$CONFIG_GRKERNSEC" = "y" ]; then
753+ source grsecurity/Config.in
754+fi
755+endmenu
756diff -urN linux-2.4.21/arch/cris/config.in linux-2.4.21/arch/cris/config.in
757--- linux-2.4.21/arch/cris/config.in 2003-06-23 11:42:34.000000000 -0400
758+++ linux-2.4.21/arch/cris/config.in 2003-06-23 11:49:15.000000000 -0400
759@@ -263,3 +263,12 @@
760
761 source lib/Config.in
762 endmenu
763+
764+mainmenu_option next_comment
765+comment 'Grsecurity'
766+bool 'Grsecurity' CONFIG_GRKERNSEC
767+if [ "$CONFIG_GRKERNSEC" = "y" ]; then
768+ source grsecurity/Config.in
769+fi
770+endmenu
771+
772diff -urN linux-2.4.21/arch/i386/config.in linux-2.4.21/arch/i386/config.in
773--- linux-2.4.21/arch/i386/config.in 2003-06-23 11:42:23.000000000 -0400
774+++ linux-2.4.21/arch/i386/config.in 2003-06-23 11:49:15.000000000 -0400
775@@ -485,3 +485,11 @@
776 endmenu
777
778 source lib/Config.in
779+
780+mainmenu_option next_comment
781+comment 'Grsecurity'
782+bool 'Grsecurity' CONFIG_GRKERNSEC
783+if [ "$CONFIG_GRKERNSEC" = "y" ]; then
784+ source grsecurity/Config.in
785+fi
786+endmenu
787diff -urN linux-2.4.21/arch/i386/kernel/i387.c linux-2.4.21/arch/i386/kernel/i387.c
788--- linux-2.4.21/arch/i386/kernel/i387.c 2003-06-23 11:42:24.000000000 -0400
789+++ linux-2.4.21/arch/i386/kernel/i387.c 2003-06-23 11:49:15.000000000 -0400
790@@ -373,13 +373,16 @@
791
792 static inline int restore_i387_fxsave( struct _fpstate *buf )
793 {
794+ int err;
795 struct task_struct *tsk = current;
796 clear_fpu( tsk );
797- if ( __copy_from_user( &tsk->thread.i387.fxsave, &buf->_fxsr_env[0],
798- sizeof(struct i387_fxsave_struct) ) )
799- return 1;
800+ err = __copy_from_user( &tsk->thread.i387.fxsave, &buf->_fxsr_env[0],
801+ sizeof(struct i387_fxsave_struct) );
802 /* mxcsr bit 6 and 31-16 must be zero for security reasons */
803 tsk->thread.i387.fxsave.mxcsr &= 0xffbf;
804+ if (err)
805+ return 1;
806+
807 return convert_fxsr_from_user( &tsk->thread.i387.fxsave, buf );
808 }
809
810diff -urN linux-2.4.21/arch/i386/kernel/ioport.c linux-2.4.21/arch/i386/kernel/ioport.c
811--- linux-2.4.21/arch/i386/kernel/ioport.c 2003-06-23 11:42:23.000000000 -0400
812+++ linux-2.4.21/arch/i386/kernel/ioport.c 2003-06-23 11:49:15.000000000 -0400
813@@ -14,6 +14,7 @@
814 #include <linux/smp.h>
815 #include <linux/smp_lock.h>
816 #include <linux/stddef.h>
817+#include <linux/grsecurity.h>
818
819 /* Set EXTENT bits starting at BASE in BITMAP to value TURN_ON. */
820 static void set_bitmap(unsigned long *bitmap, short base, short extent, int new_value)
821@@ -59,8 +60,16 @@
822
823 if ((from + num <= from) || (from + num > IO_BITMAP_SIZE*32))
824 return -EINVAL;
825+#ifdef CONFIG_GRKERNSEC_IO
826+ if (turn_on) {
827+ gr_handle_ioperm();
828+#else
829 if (turn_on && !capable(CAP_SYS_RAWIO))
830+#endif
831 return -EPERM;
832+#ifdef CONFIG_GRKERNSEC_IO
833+ }
834+#endif
835 /*
836 * If it's the first ioperm() call in this thread's lifetime, set the
837 * IO bitmap up. ioperm() is much less timing critical than clone(),
838@@ -109,8 +118,13 @@
839 return -EINVAL;
840 /* Trying to gain more privileges? */
841 if (level > old) {
842+#ifdef CONFIG_GRKERNSEC_IO
843+ gr_handle_iopl();
844+ return -EPERM;
845+#else
846 if (!capable(CAP_SYS_RAWIO))
847 return -EPERM;
848+#endif
849 }
850 regs->eflags = (regs->eflags & 0xffffcfff) | (level << 12);
851 return 0;
852diff -urN linux-2.4.21/arch/i386/kernel/ptrace.c linux-2.4.21/arch/i386/kernel/ptrace.c
853--- linux-2.4.21/arch/i386/kernel/ptrace.c 2003-06-23 11:42:24.000000000 -0400
854+++ linux-2.4.21/arch/i386/kernel/ptrace.c 2003-06-23 11:49:15.000000000 -0400
855@@ -13,6 +13,7 @@
856 #include <linux/errno.h>
857 #include <linux/ptrace.h>
858 #include <linux/user.h>
859+#include <linux/grsecurity.h>
860
861 #include <asm/uaccess.h>
862 #include <asm/pgtable.h>
863@@ -177,6 +178,9 @@
864 if (pid == 1) /* you may not mess with init */
865 goto out_tsk;
866
867+ if(gr_handle_ptrace(child, request))
868+ goto out_tsk;
869+
870 if (request == PTRACE_ATTACH) {
871 ret = ptrace_attach(child);
872 goto out_tsk;
873@@ -256,6 +260,17 @@
874 if(addr < (long) &dummy->u_debugreg[4] &&
875 ((unsigned long) data) >= TASK_SIZE-3) break;
876
877+#ifdef CONFIG_GRKERNSEC
878+ if(addr >= (long) &dummy->u_debugreg[0] &&
879+ addr <= (long) &dummy->u_debugreg[3]){
880+ long reg = (addr - (long) &dummy->u_debugreg[0]) >> 2;
881+ long type = (child->thread.debugreg[7] >> (DR_CONTROL_SHIFT + 4*reg)) & 3;
882+ long align = (child->thread.debugreg[7] >> (DR_CONTROL_SHIFT + 2 + 4*reg)) & 3;
883+ if((type & 1) && (data & align))
884+ break;
885+ }
886+#endif
887+
888 if(addr == (long) &dummy->u_debugreg[7]) {
889 data &= ~DR_CONTROL_RESERVED;
890 for(i=0; i<4; i++)
891diff -urN linux-2.4.21/arch/i386/kernel/sys_i386.c linux-2.4.21/arch/i386/kernel/sys_i386.c
892--- linux-2.4.21/arch/i386/kernel/sys_i386.c 2003-06-23 11:42:24.000000000 -0400
893+++ linux-2.4.21/arch/i386/kernel/sys_i386.c 2003-06-23 11:49:15.000000000 -0400
894@@ -18,6 +18,7 @@
895 #include <linux/mman.h>
896 #include <linux/file.h>
897 #include <linux/utsname.h>
898+#include <linux/grsecurity.h>
899
900 #include <asm/uaccess.h>
901 #include <asm/ipc.h>
902@@ -55,8 +61,14 @@
903 goto out;
904 }
905
906+ if(gr_handle_mmap(file, prot)) {
907+ fput(file);
908+ error = -EACCES;
909+ goto out;
910+ }
911+
912 down_write(&mm->mmap_sem);
913 error = do_mmap_pgoff(mm, file, addr, len, prot, flags, pgoff);
914 up_write(&mm->mmap_sem);
915
916 if (file)
917diff -urN linux-2.4.21/arch/ia64/config.in linux-2.4.21/arch/ia64/config.in
918--- linux-2.4.21/arch/ia64/config.in 2003-06-23 11:42:33.000000000 -0400
919+++ linux-2.4.21/arch/ia64/config.in 2003-06-23 11:49:15.000000000 -0400
920@@ -290,3 +290,12 @@
921 fi
922
923 endmenu
924+
925+mainmenu_option next_comment
926+comment 'Grsecurity'
927+bool 'Grsecurity' CONFIG_GRKERNSEC
928+if [ "$CONFIG_GRKERNSEC" = "y" ]; then
929+ source grsecurity/Config.in
930+fi
931+endmenu
932+
933diff -urN linux-2.4.21/arch/ia64/kernel/ptrace.c linux-2.4.21/arch/ia64/kernel/ptrace.c
934--- linux-2.4.21/arch/ia64/kernel/ptrace.c 2003-06-23 11:42:33.000000000 -0400
935+++ linux-2.4.21/arch/ia64/kernel/ptrace.c 2003-06-23 11:49:15.000000000 -0400
936@@ -16,6 +16,7 @@
937 #include <linux/ptrace.h>
938 #include <linux/smp_lock.h>
939 #include <linux/user.h>
940+#include <linux/grsecurity.h>
941
942 #include <asm/pgtable.h>
943 #include <asm/processor.h>
944@@ -1179,6 +1180,9 @@
945 if (pid == 1) /* no messing around with init! */
946 goto out_tsk;
947
948+ if (gr_handle_ptrace(child, request))
949+ goto out_tsk;
950+
951 if (request == PTRACE_ATTACH) {
952 ret = ptrace_attach(child);
953 goto out_tsk;
954diff -urN linux-2.4.21/arch/ia64/kernel/sys_ia64.c linux-2.4.21/arch/ia64/kernel/sys_ia64.c
955--- linux-2.4.21/arch/ia64/kernel/sys_ia64.c 2003-06-23 11:42:33.000000000 -0400
956+++ linux-2.4.21/arch/ia64/kernel/sys_ia64.c 2003-06-23 11:49:15.000000000 -0400
957@@ -15,6 +15,7 @@
958 #include <linux/smp.h>
959 #include <linux/smp_lock.h>
960 #include <linux/highuid.h>
961+#include <linux/grsecurity.h>
962
963 #include <asm/shmparam.h>
964 #include <asm/uaccess.h>
965@@ -212,6 +213,11 @@
966 goto out;
967 }
968
969+ if (gr_handle_mmap(file, prot)) {
970+ addr = -EACCES;
971+ goto out;
972+ }
973+
974 down_write(&current->mm->mmap_sem);
b35b0aed 975 addr = do_mmap_pgoff(current->mm, file, addr, len, prot, flags, pgoff);
dd08e044
JR
976 up_write(&current->mm->mmap_sem);
977diff -urN linux-2.4.21/arch/m68k/config.in linux-2.4.21/arch/m68k/config.in
978--- linux-2.4.21/arch/m68k/config.in 2003-06-23 11:42:29.000000000 -0400
979+++ linux-2.4.21/arch/m68k/config.in 2003-06-23 11:49:15.000000000 -0400
980@@ -563,3 +563,11 @@
981 endmenu
982
983 source lib/Config.in
984+
985+mainmenu_option next_comment
986+comment 'Grsecurity'
987+bool 'Grsecurity' CONFIG_GRKERNSEC
988+if [ "$CONFIG_GRKERNSEC" = "y" ]; then
989+ source grsecurity/Config.in
990+fi
991+endmenu
992diff -urN linux-2.4.21/arch/mips/config.in linux-2.4.21/arch/mips/config.in
993--- linux-2.4.21/arch/mips/config.in 2003-06-23 11:42:24.000000000 -0400
994+++ linux-2.4.21/arch/mips/config.in 2003-06-23 11:49:15.000000000 -0400
995@@ -7,3 +7,11 @@
996 define_bool CONFIG_MIPS64 n
997
998 source arch/mips/config-shared.in
999+
1000+mainmenu_option next_comment
1001+comment 'Grsecurity'
1002+bool 'Grsecurity' CONFIG_GRKERNSEC
1003+if [ "$CONFIG_GRKERNSEC" = "y" ]; then
1004+ source grsecurity/Config.in
1005+fi
1006+endmenu
1007diff -urN linux-2.4.21/arch/mips64/config.in linux-2.4.21/arch/mips64/config.in
1008--- linux-2.4.21/arch/mips64/config.in 2003-06-23 11:42:33.000000000 -0400
1009+++ linux-2.4.21/arch/mips64/config.in 2003-06-23 11:49:15.000000000 -0400
1010@@ -7,3 +7,11 @@
1011 define_bool CONFIG_MIPS64 y
1012
1013 source arch/mips/config-shared.in
1014+
1015+mainmenu_option next_comment
1016+comment 'Grsecurity'
1017+bool 'Grsecurity' CONFIG_GRKERNSEC
1018+if [ "$CONFIG_GRKERNSEC" = "y" ]; then
1019+ source grsecurity/Config.in
1020+fi
1021+endmenu
1022diff -urN linux-2.4.21/arch/parisc/config.in linux-2.4.21/arch/parisc/config.in
1023--- linux-2.4.21/arch/parisc/config.in 2003-06-23 11:42:33.000000000 -0400
1024+++ linux-2.4.21/arch/parisc/config.in 2003-06-23 11:49:15.000000000 -0400
1025@@ -197,3 +197,11 @@
1026 endmenu
1027
1028 source lib/Config.in
1029+
1030+mainmenu_option next_comment
1031+comment 'Grsecurity'
1032+bool 'Grsecurity' CONFIG_GRKERNSEC
1033+if [ "$CONFIG_GRKERNSEC" = "y" ]; then
1034+ source grsecurity/Config.in
1035+fi
1036+endmenu
1037diff -urN linux-2.4.21/arch/parisc/kernel/ioctl32.c linux-2.4.21/arch/parisc/kernel/ioctl32.c
1038--- linux-2.4.21/arch/parisc/kernel/ioctl32.c 2003-06-23 11:42:33.000000000 -0400
1039+++ linux-2.4.21/arch/parisc/kernel/ioctl32.c 2003-06-23 11:49:15.000000000 -0400
1040@@ -1434,7 +1434,11 @@
1041 * To have permissions to do most of the vt ioctls, we either have
1042 * to be the owner of the tty, or super-user.
1043 */
1044+#ifdef CONFIG_GRKERNSEC
1045+ if (current->tty == tty || capable(CAP_SYS_TTY_CONFIG))
1046+#else
1047 if (current->tty == tty || suser())
1048+#endif
1049 return 1;
1050 return 0;
1051 }
1052diff -urN linux-2.4.21/arch/parisc/kernel/ptrace.c linux-2.4.21/arch/parisc/kernel/ptrace.c
1053--- linux-2.4.21/arch/parisc/kernel/ptrace.c 2003-06-23 11:42:34.000000000 -0400
1054+++ linux-2.4.21/arch/parisc/kernel/ptrace.c 2003-06-23 11:49:15.000000000 -0400
1055@@ -15,7 +15,7 @@
1056 #include <linux/ptrace.h>
1057 #include <linux/user.h>
1058 #include <linux/personality.h>
1059-
1060+#include <linux/grsecurity.h>
1061 #include <asm/uaccess.h>
1062 #include <asm/pgtable.h>
1063 #include <asm/system.h>
1064@@ -119,6 +119,9 @@
1065 if (pid == 1) /* no messing around with init! */
1066 goto out_tsk;
1067
1068+ if (gr_handle_ptrace(child, request))
1069+ goto out_tsk;
1070+
1071 if (request == PTRACE_ATTACH) {
1072 ret = ptrace_attach(child);
1073 goto out_tsk;
1074diff -urN linux-2.4.21/arch/parisc/kernel/sys_parisc.c linux-2.4.21/arch/parisc/kernel/sys_parisc.c
1075--- linux-2.4.21/arch/parisc/kernel/sys_parisc.c 2003-06-23 11:42:33.000000000 -0400
1076+++ linux-2.4.21/arch/parisc/kernel/sys_parisc.c 2003-06-23 11:49:15.000000000 -0400
1077@@ -12,6 +12,7 @@
1078 #include <linux/mman.h>
1079 #include <linux/shm.h>
1080 #include <linux/smp_lock.h>
1081+#include <linux/grsecurity.h>
1082
1083 int sys_pipe(int *fildes)
1084 {
1085@@ -104,6 +110,11 @@
1086 goto out;
1087 }
1088
1089+ if (gr_handle_mmap(file, prot)) {
1090+ fput(file);
1091+ return -EACCES;
1092+ }
1093+
1094 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
1095
1096 down_write(&current->mm->mmap_sem);
1097diff -urN linux-2.4.21/arch/parisc/kernel/sys_parisc32.c linux-2.4.21/arch/parisc/kernel/sys_parisc32.c
1098--- linux-2.4.21/arch/parisc/kernel/sys_parisc32.c 2003-06-23 11:42:33.000000000 -0400
1099+++ linux-2.4.21/arch/parisc/kernel/sys_parisc32.c 2003-06-23 11:49:15.000000000 -0400
1100@@ -50,6 +50,7 @@
1101 #include <linux/highmem.h>
1102 #include <linux/highuid.h>
1103 #include <linux/mman.h>
1104+#include <linux/grsecurity.h>
1105
1106 #include <asm/types.h>
1107 #include <asm/uaccess.h>
1108@@ -177,6 +178,11 @@
1109 struct file *file;
1110 int retval;
1111 int i;
1112+#ifdef CONFIG_GRKERNSEC
1113+ struct file *old_exec_file;
1114+ struct acl_subject_label *old_acl;
1115+ struct rlimit old_rlim[RLIM_NLIMITS];
1116+#endif
1117
1118 file = open_exec(filename);
1119
1120@@ -184,6 +190,20 @@
1121 if (IS_ERR(file))
1122 return retval;
1123
1124+ gr_learn_resource(current, RLIMIT_NPROC, atomic_read(&current->user->processes));
1125+
1126+ if (gr_handle_nproc()) {
1127+ allow_write_access(file);
1128+ fput(file);
1129+ return -EAGAIN;
1130+ }
1131+
1132+ if (!gr_acl_handle_execve(file->f_dentry, file->f_vfsmnt)) {
1133+ allow_write_access(file);
1134+ fput(file);
1135+ return -EACCES;
1136+ }
1137+
1138 bprm.p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1139 memset(bprm.page, 0, MAX_ARG_PAGES*sizeof(bprm.page[0]));
1140
1141@@ -209,11 +238,24 @@
1142 if (retval < 0)
1143 goto out;
1144
1145+ if (!gr_tpe_allow(file)) {
1146+ retval = -EACCES;
1147+ goto out;
1148+ }
1149+
1150+ if (gr_check_crash_exec(file)) {
1151+ retval = -EACCES;
1152+ goto out;
1153+ }
1154+
1155 retval = copy_strings_kernel(1, &bprm.filename, &bprm);
1156 if (retval < 0)
1157 goto out;
1158
1159 bprm.exec = bprm.p;
1160+
1161+ gr_log_chroot_exec(file->f_dentry, file->f_vfsmnt);
1162+
1163 retval = copy_strings32(bprm.envc, envp, &bprm);
1164 if (retval < 0)
1165 goto out;
1166@@ -222,11 +264,31 @@
1167 if (retval < 0)
1168 goto out;
1169
1170+#ifdef CONFIG_GRKERNSEC
1171+ old_acl = current->acl;
1172+ memcpy(old_rlim, current->rlim, sizeof(old_rlim));
1173+ old_exec_file = current->exec_file;
1174+ get_file(file);
1175+ current->exec_file = file;
1176+#endif
1177+
1178+ gr_set_proc_label(file->f_dentry, file->f_vfsmnt);
1179+
1180 retval = search_binary_handler(&bprm,regs);
1181- if (retval >= 0)
1182+ if (retval >= 0) {
1183+#ifdef CONFIG_GRKERNSEC
1184+ if (old_exec_file)
1185+ fput(old_exec_file);
1186+#endif
1187 /* execve success */
1188 return retval;
1189
1190+#ifdef CONFIG_GRKERNSEC
1191+ current->acl = old_acl;
1192+ memcpy(current->rlim, old_rlim, sizeof(old_rlim));
1193+ fput(current->exec_file);
1194+ current->exec_file = old_exec_file;
1195+#endif
1196 out:
1197 /* Something went wrong, return the inode and free the argument pages*/
1198 allow_write_access(bprm.file);
1199diff -urN linux-2.4.21/arch/ppc/config.in linux-2.4.21/arch/ppc/config.in
1200--- linux-2.4.21/arch/ppc/config.in 2003-06-23 11:42:28.000000000 -0400
1201+++ linux-2.4.21/arch/ppc/config.in 2003-06-23 11:49:15.000000000 -0400
1202@@ -461,3 +461,12 @@
1203 bool 'Support for early boot texts over serial port' CONFIG_SERIAL_TEXT_DEBUG
1204 fi
1205 endmenu
1206+
1207+mainmenu_option next_comment
1208+comment 'Grsecurity'
1209+bool 'Grsecurity' CONFIG_GRKERNSEC
1210+if [ "$CONFIG_GRKERNSEC" = "y" ]; then
1211+ source grsecurity/Config.in
1212+fi
1213+endmenu
1214+
1215diff -urN linux-2.4.21/arch/ppc/kernel/ptrace.c linux-2.4.21/arch/ppc/kernel/ptrace.c
1216--- linux-2.4.21/arch/ppc/kernel/ptrace.c 2003-06-23 11:42:28.000000000 -0400
1217+++ linux-2.4.21/arch/ppc/kernel/ptrace.c 2003-06-23 11:49:15.000000000 -0400
1218@@ -24,6 +24,7 @@
1219 #include <linux/errno.h>
1220 #include <linux/ptrace.h>
1221 #include <linux/user.h>
1222+#include <linux/grsecurity.h>
1223
1224 #include <asm/uaccess.h>
1225 #include <asm/page.h>
1226@@ -182,6 +183,9 @@
1227 if (pid == 1) /* you may not mess with init */
1228 goto out_tsk;
1229
1230+ if (gr_handle_ptrace(child, request))
1231+ goto out_tsk;
1232+
1233 if (request == PTRACE_ATTACH) {
1234 ret = ptrace_attach(child);
1235 goto out_tsk;
1236diff -urN linux-2.4.21/arch/ppc/kernel/syscalls.c linux-2.4.21/arch/ppc/kernel/syscalls.c
1237--- linux-2.4.21/arch/ppc/kernel/syscalls.c 2003-06-23 11:42:28.000000000 -0400
1238+++ linux-2.4.21/arch/ppc/kernel/syscalls.c 2003-06-23 11:49:15.000000000 -0400
1239@@ -35,6 +35,7 @@
1240 #include <linux/ipc.h>
1241 #include <linux/utsname.h>
1242 #include <linux/file.h>
1243+#include <linux/grsecurity.h>
1244
1245 #include <asm/uaccess.h>
1246 #include <asm/ipc.h>
1247@@ -181,8 +182,14 @@
1248 goto out;
1249 }
1250
1251+ if (gr_handle_mmap(file, prot)) {
1252+ fput(file);
1253+ ret = -EACCES;
1254+ goto out;
1255+ }
1256+
1257 down_write(&current->mm->mmap_sem);
b35b0aed 1258 ret = do_mmap_pgoff(current->mm, file, addr, len, prot, flags, pgoff);
dd08e044
JR
1259 up_write(&current->mm->mmap_sem);
1260 if (file)
1261 fput(file);
1262diff -urN linux-2.4.21/arch/ppc64/kernel/ioctl32.c linux-2.4.21/arch/ppc64/kernel/ioctl32.c
1263--- linux-2.4.21/arch/ppc64/kernel/ioctl32.c 2003-06-23 11:42:23.000000000 -0400
1264+++ linux-2.4.21/arch/ppc64/kernel/ioctl32.c 2003-06-23 11:49:15.000000000 -0400
1265@@ -1789,7 +1789,11 @@
1266 * To have permissions to do most of the vt ioctls, we either have
1267 * to be the owner of the tty, or super-user.
1268 */
1269+#ifdef CONFIG_GRKERNSEC
1270+ if (current->tty == tty || capable(CAP_SYS_TTY_CONFIG))
1271+#else
1272 if (current->tty == tty || suser())
1273+#endif
1274 return 1;
1275 return 0;
1276 }
1277diff -urN linux-2.4.21/arch/s390/config.in linux-2.4.21/arch/s390/config.in
1278--- linux-2.4.21/arch/s390/config.in 2003-06-23 11:42:33.000000000 -0400
1279+++ linux-2.4.21/arch/s390/config.in 2003-06-23 11:49:15.000000000 -0400
1280@@ -76,3 +76,11 @@
1281 endmenu
1282
1283 source lib/Config.in
1284+
1285+mainmenu_option next_comment
1286+comment 'Grsecurity'
1287+bool 'Grsecurity' CONFIG_GRKERNSEC
1288+if [ "$CONFIG_GRKERNSEC" = "y" ]; then
1289+ source grsecurity/Config.in
1290+fi
1291+endmenu
1292diff -urN linux-2.4.21/arch/s390x/config.in linux-2.4.21/arch/s390x/config.in
1293--- linux-2.4.21/arch/s390x/config.in 2003-06-23 11:42:34.000000000 -0400
1294+++ linux-2.4.21/arch/s390x/config.in 2003-06-23 11:49:15.000000000 -0400
1295@@ -80,3 +80,11 @@
1296 endmenu
1297
1298 source lib/Config.in
1299+
1300+mainmenu_option next_comment
1301+comment 'Grsecurity'
1302+bool 'Grsecurity' CONFIG_GRKERNSEC
1303+if [ "$CONFIG_GRKERNSEC" = "y" ]; then
1304+ source grsecurity/Config.in
1305+fi
1306+endmenu
1307diff -urN linux-2.4.21/arch/sh/config.in linux-2.4.21/arch/sh/config.in
1308--- linux-2.4.21/arch/sh/config.in 2003-06-23 11:42:32.000000000 -0400
1309+++ linux-2.4.21/arch/sh/config.in 2003-06-23 11:49:15.000000000 -0400
1310@@ -388,3 +388,11 @@
1311 endmenu
1312
1313 source lib/Config.in
1314+
1315+mainmenu_option next_comment
1316+comment 'Grsecurity'
1317+bool 'Grsecurity' CONFIG_GRKERNSEC
1318+if [ "$CONFIG_GRKERNSEC" = "y" ]; then
1319+ source grsecurity/Config.in
1320+fi
1321+endmenu
1322diff -urN linux-2.4.21/arch/sparc/boot/Makefile linux-2.4.21/arch/sparc/boot/Makefile
1323--- linux-2.4.21/arch/sparc/boot/Makefile 2003-06-23 11:42:24.000000000 -0400
1324+++ linux-2.4.21/arch/sparc/boot/Makefile 2003-06-23 11:49:15.000000000 -0400
1325@@ -24,7 +24,7 @@
1326
1327 BTOBJS := $(HEAD) init/main.o init/version.o init/do_mounts.o
1328 BTLIBS := $(CORE_FILES_NO_BTFIX) $(FILESYSTEMS) \
1329- $(DRIVERS) $(NETWORKS)
1330+ $(DRIVERS) $(NETWORKS) $(GRSECURITY)
1331
1332 # I wanted to make this depend upon BTOBJS so that a parallel
1333 # build would work, but this fails because $(HEAD) cannot work
1334diff -urN linux-2.4.21/arch/sparc/config.in linux-2.4.21/arch/sparc/config.in
1335--- linux-2.4.21/arch/sparc/config.in 2003-06-23 11:42:24.000000000 -0400
1336+++ linux-2.4.21/arch/sparc/config.in 2003-06-23 11:49:15.000000000 -0400
1337@@ -276,3 +276,11 @@
1338 endmenu
1339
1340 source lib/Config.in
1341+
1342+mainmenu_option next_comment
1343+comment 'Grsecurity'
1344+bool 'Grsecurity' CONFIG_GRKERNSEC
1345+if [ "$CONFIG_GRKERNSEC" = "y" ]; then
1346+ source grsecurity/Config.in
1347+fi
1348+endmenu
1349diff -urN linux-2.4.21/arch/sparc/kernel/ptrace.c linux-2.4.21/arch/sparc/kernel/ptrace.c
1350--- linux-2.4.21/arch/sparc/kernel/ptrace.c 2003-06-23 11:42:24.000000000 -0400
1351+++ linux-2.4.21/arch/sparc/kernel/ptrace.c 2003-06-23 11:49:15.000000000 -0400
1352@@ -17,6 +17,7 @@
1353 #include <linux/user.h>
1354 #include <linux/smp.h>
1355 #include <linux/smp_lock.h>
1356+#include <linux/grsecurity.h>
1357
1358 #include <asm/pgtable.h>
1359 #include <asm/system.h>
1360@@ -310,6 +311,9 @@
1361 goto out;
1362 }
1363
1364+ if(gr_handle_ptrace(child, request))
1365+ goto out_tsk;
1366+
1367 if ((current->personality == PER_SUNOS && request == PTRACE_SUNATTACH)
1368 || (current->personality != PER_SUNOS && request == PTRACE_ATTACH)) {
1369 if (ptrace_attach(child)) {
1370diff -urN linux-2.4.21/arch/sparc/kernel/sys_sparc.c linux-2.4.21/arch/sparc/kernel/sys_sparc.c
1371--- linux-2.4.21/arch/sparc/kernel/sys_sparc.c 2003-06-23 11:42:24.000000000 -0400
1372+++ linux-2.4.21/arch/sparc/kernel/sys_sparc.c 2003-06-23 11:49:15.000000000 -0400
1373@@ -20,6 +20,7 @@
1374 #include <linux/utsname.h>
1375 #include <linux/smp.h>
1376 #include <linux/smp_lock.h>
1377+#include <linux/grsecurity.h>
1378
1379 #include <asm/uaccess.h>
1380 #include <asm/ipc.h>
1381@@ -240,6 +253,12 @@
1382 if (len > TASK_SIZE - PAGE_SIZE || addr + len > TASK_SIZE - PAGE_SIZE)
1383 goto out_putf;
1384
1385+ if (gr_handle_mmap(file, prot)) {
1386+ fput(file);
1387+ retval = -EACCES;
1388+ goto out;
1389+ }
1390+
1391 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
1392
1393 down_write(&current->mm->mmap_sem);
1394diff -urN linux-2.4.21/arch/sparc64/config.in linux-2.4.21/arch/sparc64/config.in
1395--- linux-2.4.21/arch/sparc64/config.in 2003-06-23 11:42:30.000000000 -0400
1396+++ linux-2.4.21/arch/sparc64/config.in 2003-06-23 11:49:15.000000000 -0400
1397@@ -310,3 +310,11 @@
1398 endmenu
1399
1400 source lib/Config.in
1401+
1402+mainmenu_option next_comment
1403+comment 'Grsecurity'
1404+bool 'Grsecurity' CONFIG_GRKERNSEC
1405+if [ "$CONFIG_GRKERNSEC" = "y" ]; then
1406+ source grsecurity/Config.in
1407+fi
1408+endmenu
1409diff -urN linux-2.4.21/arch/sparc64/kernel/ioctl32.c linux-2.4.21/arch/sparc64/kernel/ioctl32.c
1410--- linux-2.4.21/arch/sparc64/kernel/ioctl32.c 2003-06-23 11:42:30.000000000 -0400
1411+++ linux-2.4.21/arch/sparc64/kernel/ioctl32.c 2003-06-23 11:49:15.000000000 -0400
1412@@ -2046,7 +2046,11 @@
1413 * To have permissions to do most of the vt ioctls, we either have
1414 * to be the owner of the tty, or super-user.
1415 */
1416+#ifdef CONFIG_GRKERNSEC
1417+ if (current->tty == tty || capable(CAP_SYS_TTY_CONFIG))
1418+#else
1419 if (current->tty == tty || suser())
1420+#endif
1421 return 1;
1422 return 0;
1423 }
1424diff -urN linux-2.4.21/arch/sparc64/kernel/ptrace.c linux-2.4.21/arch/sparc64/kernel/ptrace.c
1425--- linux-2.4.21/arch/sparc64/kernel/ptrace.c 2003-06-23 11:42:30.000000000 -0400
1426+++ linux-2.4.21/arch/sparc64/kernel/ptrace.c 2003-06-23 11:49:15.000000000 -0400
1427@@ -18,6 +18,7 @@
1428 #include <linux/user.h>
1429 #include <linux/smp.h>
1430 #include <linux/smp_lock.h>
1431+#include <linux/grsecurity.h>
1432
1433 #include <asm/asi.h>
1434 #include <asm/pgtable.h>
1435@@ -161,6 +162,11 @@
1436 goto out;
1437 }
1438
1439+ if (gr_handle_ptrace(child, (long)request)) {
1440+ pt_error_return(regs, EPERM);
1441+ goto out;
1442+ }
1443+
1444 if ((current->personality == PER_SUNOS && request == PTRACE_SUNATTACH)
1445 || (current->personality != PER_SUNOS && request == PTRACE_ATTACH)) {
1446 if (ptrace_attach(child)) {
1447diff -urN linux-2.4.21/arch/sparc64/kernel/sys_sparc.c linux-2.4.21/arch/sparc64/kernel/sys_sparc.c
1448--- linux-2.4.21/arch/sparc64/kernel/sys_sparc.c 2003-06-23 11:42:30.000000000 -0400
1449+++ linux-2.4.21/arch/sparc64/kernel/sys_sparc.c 2003-06-23 11:49:15.000000000 -0400
1450@@ -24,6 +24,7 @@
1451 #include <linux/slab.h>
1452 #include <linux/ipc.h>
1453 #include <linux/personality.h>
1454+#include <linux/grsecurity.h>
1455
1456 #include <asm/uaccess.h>
1457 #include <asm/ipc.h>
1458@@ -280,6 +288,12 @@
1459 if (!file)
1460 goto out;
1461 }
1462+
1463+ if (gr_handle_mmap(file, prot)) {
1464+ retval = -EACCES;
1465+ goto out_putf;
1466+ }
1467+
1468 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
1469 len = PAGE_ALIGN(len);
1470 retval = -EINVAL;
1471diff -urN linux-2.4.21/arch/sparc64/kernel/sys_sparc32.c linux-2.4.21/arch/sparc64/kernel/sys_sparc32.c
1472--- linux-2.4.21/arch/sparc64/kernel/sys_sparc32.c 2003-06-23 11:42:30.000000000 -0400
1473+++ linux-2.4.21/arch/sparc64/kernel/sys_sparc32.c 2003-06-23 11:49:15.000000000 -0400
1474@@ -51,6 +51,8 @@
1475 #include <linux/sysctl.h>
1476 #include <linux/dnotify.h>
1477 #include <linux/netfilter_ipv4/ip_tables.h>
1478+#include <linux/random.h>
1479+#include <linux/grsecurity.h>
1480
1481 #include <asm/types.h>
1482 #include <asm/ipc.h>
1483@@ -3203,6 +3205,11 @@
1484 struct file * file;
1485 int retval;
1486 int i;
1487+#ifdef CONFIG_GRKERNSEC
1488+ struct file *old_exec_file;
1489+ struct acl_subject_label *old_acl;
1490+ struct rlimit old_rlim[RLIM_NLIMITS];
1491+#endif
1492
1493 bprm.p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1494 memset(bprm.page, 0, MAX_ARG_PAGES * sizeof(bprm.page[0]));
1495@@ -3213,6 +3229,20 @@
1496 if (IS_ERR(file))
1497 return retval;
1498
1499+ gr_learn_resource(current, RLIMIT_NPROC, atomic_read(&current->user->processes));
1500+
1501+ if (gr_handle_nproc()) {
1502+ allow_write_access(file);
1503+ fput(file);
1504+ return -EAGAIN;
1505+ }
1506+
1507+ if (!gr_acl_handle_execve(file->f_dentry, file->f_vfsmnt)) {
1508+ allow_write_access(file);
1509+ fput(file);
1510+ return -EACCES;
1511+ }
1512+
1513 bprm.file = file;
1514 bprm.filename = filename;
1515 bprm.sh_bang = 0;
1516@@ -3233,11 +3263,24 @@
1517 if (retval < 0)
1518 goto out;
1519
1520+ if(!gr_tpe_allow(file)) {
1521+ retval = -EACCES;
1522+ goto out;
1523+ }
1524+
1525+ if (gr_check_crash_exec(file)) {
1526+ retval = -EACCES;
1527+ goto out;
1528+ }
1529+
1530 retval = copy_strings_kernel(1, &bprm.filename, &bprm);
1531 if (retval < 0)
1532 goto out;
1533
1534 bprm.exec = bprm.p;
1535+
1536+ gr_log_chroot_exec(file->f_dentry, file->f_vfsmnt);
1537+
1538 retval = copy_strings32(bprm.envc, envp, &bprm);
1539 if (retval < 0)
1540 goto out;
1541@@ -3246,11 +3289,32 @@
1542 if (retval < 0)
1543 goto out;
1544
1545+#ifdef CONFIG_GRKERNSEC
1546+ old_acl = current->acl;
1547+ memcpy(old_rlim, current->rlim, sizeof(old_rlim));
1548+ old_exec_file = current->exec_file;
1549+ get_file(file);
1550+ current->exec_file = file;
1551+#endif
1552+
1553+ gr_set_proc_label(file->f_dentry, file->f_vfsmnt);
1554+
1555 retval = search_binary_handler(&bprm, regs);
1556- if (retval >= 0)
1557+ if (retval >= 0) {
1558+#ifdef CONFIG_GRKERNSEC
1559+ if (old_exec_file)
1560+ fput(old_exec_file);
1561+#endif
1562 /* execve success */
1563 return retval;
1564+ }
1565
1566+#ifdef CONFIG_GRKERNSEC
1567+ current->acl = old_acl;
1568+ memcpy(current->rlim, old_rlim, sizeof(old_rlim));
1569+ fput(current->exec_file);
1570+ current->exec_file = old_exec_file;
1571+#endif
1572 out:
1573 /* Something went wrong, return the inode and free the argument pages*/
1574 allow_write_access(bprm.file);
1575diff -urN linux-2.4.21/arch/x86_64/ia32/ia32_ioctl.c linux-2.4.21/arch/x86_64/ia32/ia32_ioctl.c
1576--- linux-2.4.21/arch/x86_64/ia32/ia32_ioctl.c 2003-06-23 11:42:23.000000000 -0400
1577+++ linux-2.4.21/arch/x86_64/ia32/ia32_ioctl.c 2003-06-23 11:49:15.000000000 -0400
1578@@ -1932,7 +1932,11 @@
1579 * To have permissions to do most of the vt ioctls, we either have
1580 * to be the owner of the tty, or super-user.
1581 */
1582+#ifdef CONFIG_GRKERNSEC
1583+ if (current->tty == tty || capable(CAP_SYS_TTY_CONFIG))
1584+#else
1585 if (current->tty == tty || suser())
1586+#endif
1587 return 1;
1588 return 0;
1589 }
1590diff -urN linux-2.4.21/drivers/char/mem.c linux-2.4.21/drivers/char/mem.c
1591--- linux-2.4.21/drivers/char/mem.c 2003-06-23 11:42:01.000000000 -0400
1592+++ linux-2.4.21/drivers/char/mem.c 2003-06-23 11:49:15.000000000 -0400
1593@@ -21,6 +21,7 @@
1594 #include <linux/raw.h>
1595 #include <linux/tty.h>
1596 #include <linux/capability.h>
1597+#include <linux/grsecurity.h>
1598
1599 #include <asm/uaccess.h>
1600 #include <asm/io.h>
1601@@ -41,6 +42,10 @@
1602 #if defined(CONFIG_S390_TAPE) && defined(CONFIG_S390_TAPE_CHAR)
1603 extern void tapechar_init(void);
1604 #endif
1605+
1606+#ifdef CONFIG_GRKERNSEC
1607+extern struct file_operations grsec_fops;
1608+#endif
1609
1610 static ssize_t do_write_mem(struct file * file, void *p, unsigned long realp,
1611 const char * buf, size_t count, loff_t *ppos)
1612@@ -114,6 +119,11 @@
1613 unsigned long p = *ppos;
1614 unsigned long end_mem;
1615
1616+#ifdef CONFIG_GRKERNSEC_KMEM
1617+ gr_handle_mem_write();
1618+ return -EPERM;
1619+#endif
1620+
1621 end_mem = __pa(high_memory);
1622 if (p >= end_mem)
1623 return 0;
1624@@ -186,6 +196,12 @@
1625 {
1626 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
1627
1628+#ifdef CONFIG_GRKERNSEC_KMEM
1629+ if (gr_handle_mem_mmap(offset, vma))
1630+ return -EPERM;
1631+#endif
1632+
1633+
1634 /*
1635 * Accessing memory above the top the kernel knows about or
1636 * through a file pointer that was marked O_SYNC will be
1637@@ -285,6 +301,11 @@
1638 ssize_t virtr = 0;
1639 char * kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
1640
1641+#ifdef CONFIG_GRKERNSEC_KMEM
1642+ gr_handle_kmem_write();
1643+ return -EPERM;
1644+#endif
1645+
1646 if (p < (unsigned long) high_memory) {
1647 wrote = count;
1648 if (count > (unsigned long) high_memory - p)
1649@@ -517,6 +538,15 @@
1650
1651 static int open_port(struct inode * inode, struct file * filp)
1652 {
1653+#ifdef CONFIG_GRKERNSEC_KMEM
1654+ gr_handle_open_port();
1655+ return -EPERM;
1656+#endif
1657+ return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
1658+}
1659+
1660+static int open_mem(struct inode * inode, struct file * filp)
1661+{
1662 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
1663 }
1664
1665@@ -609,7 +639,6 @@
1666 #define full_lseek null_lseek
1667 #define write_zero write_null
1668 #define read_full read_zero
1669-#define open_mem open_port
1670 #define open_kmem open_mem
1671
1672 static struct file_operations mem_fops = {
1673@@ -685,6 +714,11 @@
1674 case 9:
1675 filp->f_op = &urandom_fops;
1676 break;
1677+#ifdef CONFIG_GRKERNSEC
1678+ case 10:
1679+ filp->f_op = &grsec_fops;
1680+ break;
1681+#endif
1682 default:
1683 return -ENXIO;
1684 }
1685@@ -711,7 +745,10 @@
1686 {5, "zero", S_IRUGO | S_IWUGO, &zero_fops},
1687 {7, "full", S_IRUGO | S_IWUGO, &full_fops},
1688 {8, "random", S_IRUGO | S_IWUSR, &random_fops},
1689- {9, "urandom", S_IRUGO | S_IWUSR, &urandom_fops}
1690+ {9, "urandom", S_IRUGO | S_IWUSR, &urandom_fops},
1691+#ifdef CONFIG_GRKERNSEC
1692+ {10,"grsec", S_IRUSR | S_IWUGO, &grsec_fops}
1693+#endif
1694 };
1695 int i;
1696
1697diff -urN linux-2.4.21/drivers/char/random.c linux-2.4.21/drivers/char/random.c
1698--- linux-2.4.21/drivers/char/random.c 2003-06-23 11:42:01.000000000 -0400
1699+++ linux-2.4.21/drivers/char/random.c 2003-06-23 11:49:15.000000000 -0400
1700@@ -260,9 +260,15 @@
1701 /*
1702 * Configuration information
1703 */
1704+#ifdef CONFIG_GRKERNSEC_RANDNET
1705+#define DEFAULT_POOL_SIZE 1024
1706+#define SECONDARY_POOL_SIZE 256
1707+#define BATCH_ENTROPY_SIZE 512
1708+#else
1709 #define DEFAULT_POOL_SIZE 512
1710 #define SECONDARY_POOL_SIZE 128
1711 #define BATCH_ENTROPY_SIZE 256
1712+#endif
1713 #define USE_SHA
1714
1715 /*
1716@@ -387,6 +393,7 @@
1717 /*
1718 * Static global variables
1719 */
1720+
1721 static struct entropy_store *random_state; /* The default global store */
1722 static struct entropy_store *sec_random_state; /* secondary store */
1723 static DECLARE_WAIT_QUEUE_HEAD(random_read_wait);
1724diff -urN linux-2.4.21/drivers/char/tty_io.c linux-2.4.21/drivers/char/tty_io.c
1725--- linux-2.4.21/drivers/char/tty_io.c 2003-06-23 11:42:01.000000000 -0400
1726+++ linux-2.4.21/drivers/char/tty_io.c 2003-06-23 11:49:15.000000000 -0400
1727@@ -99,7 +99,7 @@
1728 #include <linux/vt_kern.h>
1729 #include <linux/selection.h>
1730 #include <linux/devfs_fs_kernel.h>
1731-
1732+#include <linux/grsecurity.h>
1733 #include <linux/kmod.h>
1734
1735 #ifdef CONFIG_VT
1736@@ -1397,7 +1397,11 @@
1737 retval = -ENODEV;
1738 filp->f_flags = saved_flags;
1739
1740+#ifdef CONFIG_GRKERNSEC
1741+ if (!retval && test_bit(TTY_EXCLUSIVE, &tty->flags) && !capable(CAP_SYS_TTY_CONFIG))
1742+#else
1743 if (!retval && test_bit(TTY_EXCLUSIVE, &tty->flags) && !suser())
1744+#endif
1745 retval = -EBUSY;
1746
1747 if (retval) {
1748@@ -1499,7 +1503,11 @@
1749 {
1750 char ch, mbz = 0;
1751
1752+#ifdef CONFIG_GRKERNSEC
1753+ if ((current->tty != tty) && !capable(CAP_SYS_TTY_CONFIG))
1754+#else
1755 if ((current->tty != tty) && !suser())
1756+#endif
1757 return -EPERM;
1758 if (get_user(ch, arg))
1759 return -EFAULT;
1760@@ -1537,7 +1545,11 @@
1761 if (inode->i_rdev == SYSCONS_DEV ||
1762 inode->i_rdev == CONSOLE_DEV) {
1763 struct file *f;
1764+#ifdef CONFIG_GRKERNSEC
1765+ if (!capable(CAP_SYS_TTY_CONFIG))
1766+#else
1767 if (!suser())
1768+#endif
1769 return -EPERM;
1770 spin_lock(&redirect_lock);
1771 f = redirect;
1772@@ -1589,7 +1601,11 @@
1773 * This tty is already the controlling
1774 * tty for another session group!
1775 */
1776+#ifdef CONFIG_GRKERNSEC
1777+ if ((arg == 1) && capable(CAP_SYS_ADMIN)) {
1778+#else
1779 if ((arg == 1) && suser()) {
1780+#endif
1781 /*
1782 * Steal it away
1783 */
1784diff -urN linux-2.4.21/drivers/char/vt.c linux-2.4.21/drivers/char/vt.c
1785--- linux-2.4.21/drivers/char/vt.c 2003-06-23 11:42:01.000000000 -0400
1786+++ linux-2.4.21/drivers/char/vt.c 2003-06-23 11:49:15.000000000 -0400
1787@@ -32,6 +32,7 @@
1788 #include <linux/vt_kern.h>
1789 #include <linux/kbd_diacr.h>
1790 #include <linux/selection.h>
1791+#include <linux/grsecurity.h>
1792
1793 #ifdef CONFIG_FB_COMPAT_XPMAC
1794 #include <asm/vc_ioctl.h>
1795@@ -443,7 +444,11 @@
1796 * to be the owner of the tty, or super-user.
1797 */
1798 perm = 0;
1799+#ifdef CONFIG_GRKERNSEC
1800+ if (current->tty == tty || capable(CAP_SYS_TTY_CONFIG))
1801+#else
1802 if (current->tty == tty || suser())
1803+#endif
1804 perm = 1;
1805
1806 kbd = kbd_table + console;
1807@@ -1038,12 +1043,20 @@
1808 return do_unimap_ioctl(cmd, (struct unimapdesc *)arg, perm);
1809
1810 case VT_LOCKSWITCH:
1811+#ifdef CONFIG_GRKERNSEC
1812+ if (!capable(CAP_SYS_TTY_CONFIG))
1813+#else
1814 if (!suser())
1815+#endif
1816 return -EPERM;
1817 vt_dont_switch = 1;
1818 return 0;
1819 case VT_UNLOCKSWITCH:
1820+#ifdef CONFIG_GRKERNSEC
1821+ if (!capable(CAP_SYS_TTY_CONFIG))
1822+#else
1823 if (!suser())
1824+#endif
1825 return -EPERM;
1826 vt_dont_switch = 0;
1827 return 0;
1828diff -urN linux-2.4.21/drivers/pci/proc.c linux-2.4.21/drivers/pci/proc.c
1829--- linux-2.4.21/drivers/pci/proc.c 2003-06-23 11:42:12.000000000 -0400
1830+++ linux-2.4.21/drivers/pci/proc.c 2003-06-23 11:49:15.000000000 -0400
1831@@ -562,7 +562,15 @@
1832 pci_for_each_dev(dev) {
1833 pci_proc_attach_device(dev);
1834 }
1835+#ifdef CONFIG_GRKERNSEC_PROC_ADD
1836+#ifdef CONFIG_GRKERNSEC_PROC_USER
1837+ entry = create_proc_entry("pci", S_IRUSR, NULL);
1838+#elif CONFIG_GRKERNSEC_PROC_USERGROUP
1839+ entry = create_proc_entry("pci", S_IRUSR | S_IRGRP, NULL);
1840+#endif
1841+#else
1842 entry = create_proc_entry("pci", 0, NULL);
1843+#endif
1844 if (entry)
1845 entry->proc_fops = &proc_pci_operations;
1846 }
1847diff -urN linux-2.4.21/fs/binfmt_aout.c linux-2.4.21/fs/binfmt_aout.c
1848--- linux-2.4.21/fs/binfmt_aout.c 2003-06-23 11:41:29.000000000 -0400
1849+++ linux-2.4.21/fs/binfmt_aout.c 2003-06-23 11:49:16.000000000 -0400
1850@@ -5,6 +5,7 @@
1851 */
1852
1853 #include <linux/module.h>
1854+#include <linux/config.h>
1855
1856 #include <linux/sched.h>
1857 #include <linux/kernel.h>
1858@@ -113,10 +114,12 @@
1859 /* If the size of the dump file exceeds the rlimit, then see what would happen
1860 if we wrote the stack, but not the data area. */
1861 #ifdef __sparc__
1862+ gr_learn_resource(current, RLIMIT_CORE, dump.u_dsize+dump.u_ssize);
1863 if ((dump.u_dsize+dump.u_ssize) >
1864 current->rlim[RLIMIT_CORE].rlim_cur)
1865 dump.u_dsize = 0;
1866 #else
1867+ gr_learn_resource(current, RLIMIT_CORE, (dump.u_dsize+dump.u_ssize+1) * PAGE_SIZE);
1868 if ((dump.u_dsize+dump.u_ssize+1) * PAGE_SIZE >
1869 current->rlim[RLIMIT_CORE].rlim_cur)
1870 dump.u_dsize = 0;
1871@@ -124,10 +127,12 @@
1872
1873 /* Make sure we have enough room to write the stack and data areas. */
1874 #ifdef __sparc__
1875+ gr_learn_resource(current, RLIMIT_CORE, dump.u_ssize);
1876 if ((dump.u_ssize) >
1877 current->rlim[RLIMIT_CORE].rlim_cur)
1878 dump.u_ssize = 0;
1879 #else
1880+ gr_learn_resource(current, RLIMIT_CORE, (dump.u_ssize+1) * PAGE_SIZE);
1881 if ((dump.u_ssize+1) * PAGE_SIZE >
1882 current->rlim[RLIMIT_CORE].rlim_cur)
1883 dump.u_ssize = 0;
1884@@ -276,6 +281,8 @@
1885 rlim = current->rlim[RLIMIT_DATA].rlim_cur;
1886 if (rlim >= RLIM_INFINITY)
1887 rlim = ~0;
1888+
1889+ gr_learn_resource(current, RLIMIT_DATA, ex.a_data + ex.a_bss);
1890 if (ex.a_data + ex.a_bss > rlim)
1891 return -ENOMEM;
1892
1893diff -urN linux-2.4.21/fs/binfmt_elf.c linux-2.4.21/fs/binfmt_elf.c
1894--- linux-2.4.21/fs/binfmt_elf.c 2003-06-23 11:41:29.000000000 -0400
1895+++ linux-2.4.21/fs/binfmt_elf.c 2003-06-23 11:49:16.000000000 -0400
1896@@ -11,6 +11,7 @@
1897
1898 #include <linux/module.h>
1899
1900+#include <linux/config.h>
1901 #include <linux/fs.h>
1902 #include <linux/stat.h>
1903 #include <linux/sched.h>
1904@@ -33,6 +34,7 @@
1905 #include <linux/smp_lock.h>
1906 #include <linux/compiler.h>
1907 #include <linux/highmem.h>
1908+#include <linux/grsecurity.h>
1909
1910 #include <asm/uaccess.h>
1911 #include <asm/param.h>
1912@@ -1003,8 +1163,11 @@
1913 #undef DUMP_SEEK
1914
1915 #define DUMP_WRITE(addr, nr) \
1916+ do { \
1917+ gr_learn_resource(current, RLIMIT_CORE, size + (nr)); \
1918 if ((size += (nr)) > limit || !dump_write(file, (addr), (nr))) \
1919- goto end_coredump;
1920+ goto end_coredump; \
1921+ } while (0);
1922 #define DUMP_SEEK(off) \
1923 if (!dump_seek(file, (off))) \
1924 goto end_coredump;
1925diff -urN linux-2.4.21/fs/buffer.c linux-2.4.21/fs/buffer.c
1926--- linux-2.4.21/fs/buffer.c 2003-06-23 11:41:28.000000000 -0400
1927+++ linux-2.4.21/fs/buffer.c 2003-06-23 11:49:16.000000000 -0400
1928@@ -1781,6 +1781,9 @@
1929 int err;
1930
1931 err = -EFBIG;
1932+
1933+ gr_learn_resource(current, RLIMIT_FSIZE, (unsigned long) size);
1934+
1935 limit = current->rlim[RLIMIT_FSIZE].rlim_cur;
1936 if (limit != RLIM_INFINITY && size > (loff_t)limit) {
1937 send_sig(SIGXFSZ, current, 0);
1938diff -urN linux-2.4.21/fs/exec.c linux-2.4.21/fs/exec.c
1939--- linux-2.4.21/fs/exec.c 2003-06-23 11:41:28.000000000 -0400
1940+++ linux-2.4.21/fs/exec.c 2003-06-23 11:49:16.000000000 -0400
1941@@ -43,6 +43,8 @@
1942 #include <asm/uaccess.h>
1943 #include <asm/pgalloc.h>
1944 #include <asm/mmu_context.h>
1945+#include <linux/major.h>
1946+#include <linux/grsecurity.h>
1947
1948 #ifdef CONFIG_KMOD
1949 #include <linux/kmod.h>
1950@@ -687,6 +725,9 @@
1951 cap_set_full(bprm->cap_effective);
1952 }
1953
1954+ if (gr_handle_ptrace_exec(bprm->file->f_dentry, bprm->file->f_vfsmnt))
1955+ return -EACCES;
1956+
1957 memset(bprm->buf,0,BINPRM_BUF_SIZE);
1958 return kernel_read(bprm->file,0,bprm->buf,BINPRM_BUF_SIZE);
1959 }
1960@@ -752,6 +793,8 @@
1961 current->suid = current->euid = current->fsuid = bprm->e_uid;
1962 current->sgid = current->egid = current->fsgid = bprm->e_gid;
1963
1964+ gr_handle_chroot_caps(current);
1965+
1966 if(do_unlock)
1967 unlock_kernel();
1968 current->keep_capabilities = 0;
1969@@ -885,6 +928,11 @@
1970 struct file *file;
1971 int retval;
1972 int i;
1973+#ifdef CONFIG_GRKERNSEC
1974+ struct file *old_exec_file;
1975+ struct acl_subject_label *old_acl;
1976+ struct rlimit old_rlim[RLIM_NLIMITS];
1977+#endif
1978
1979 file = open_exec(filename);
1980
1981@@ -892,7 +940,21 @@
1982 if (IS_ERR(file))
1983 return retval;
1984
1985+ gr_learn_resource(current, RLIMIT_NPROC, atomic_read(&current->user->processes));
1986+
1987+ if (gr_handle_nproc()) {
1988+ allow_write_access(file);
1989+ fput(file);
1990+ return -EAGAIN;
1991+ }
1992+
1993+ if (!gr_acl_handle_execve(file->f_dentry, file->f_vfsmnt)) {
1994+ allow_write_access(file);
1995+ fput(file);
1996+ return -EACCES;
1997+ }
1998+
1999 bprm.p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
2000 memset(bprm.page, 0, MAX_ARG_PAGES*sizeof(bprm.page[0]));
2001
2002 bprm.file = file;
2003@@ -916,11 +987,26 @@
2004 if (retval < 0)
2005 goto out;
2006
2007+ if (!gr_tpe_allow(file)) {
2008+ retval = -EACCES;
2009+ goto out;
2010+ }
2011+
2012+ if(gr_check_crash_exec(file)) {
2013+ retval = -EACCES;
2014+ goto out;
2015+ }
2016+
2017 retval = copy_strings_kernel(1, &bprm.filename, &bprm);
2018 if (retval < 0)
2019 goto out;
2020
2021 bprm.exec = bprm.p;
2022+
2023+ gr_log_chroot_exec(file->f_dentry, file->f_vfsmnt);
2024+
2025+ gr_handle_exec_args(&bprm, argv);
2026+
2027 retval = copy_strings(bprm.envc, envp, &bprm);
2028 if (retval < 0)
2029 goto out;
2030@@ -929,11 +1015,32 @@
2031 if (retval < 0)
2032 goto out;
2033
2034+#ifdef CONFIG_GRKERNSEC
2035+ old_acl = current->acl;
2036+ memcpy(old_rlim, current->rlim, sizeof(old_rlim));
2037+ old_exec_file = current->exec_file;
2038+ get_file(file);
2039+ current->exec_file = file;
2040+#endif
2041+
2042+ gr_set_proc_label(file->f_dentry, file->f_vfsmnt);
2043+
2044 retval = search_binary_handler(&bprm,regs);
2045- if (retval >= 0)
2046+ if (retval >= 0) {
2047+#ifdef CONFIG_GRKERNSEC
2048+ if (old_exec_file)
2049+ fput(old_exec_file);
2050+#endif
2051 /* execve success */
2052 return retval;
2053+ }
2054
2055+#ifdef CONFIG_GRKERNSEC
2056+ current->acl = old_acl;
2057+ memcpy(current->rlim, old_rlim, sizeof(old_rlim));
2058+ fput(current->exec_file);
2059+ current->exec_file = old_exec_file;
2060+#endif
2061 out:
2062 /* Something went wrong, return the inode and free the argument pages*/
2063 allow_write_access(bprm.file);
2064@@ -1090,6 +1234,7 @@
2065 if (!is_dumpable(current))
2066 goto fail;
2067 current->mm->dumpable = 0;
2068+ gr_learn_resource(current, RLIMIT_CORE, binfmt->min_coredump);
2069 if (current->rlim[RLIMIT_CORE].rlim_cur < binfmt->min_coredump)
2070 goto fail;
2071
2072@@ -1109,7 +1254,7 @@
2073 goto close_fail;
2074 if (!file->f_op->write)
2075 goto close_fail;
2076- if (do_truncate(file->f_dentry, 0) != 0)
2077+ if (do_truncate(file->f_dentry, 0, file->f_vfsmnt) != 0)
2078 goto close_fail;
2079
2080 retval = binfmt->core_dump(signr, regs, file);
2081diff -urN linux-2.4.21/fs/fcntl.c linux-2.4.21/fs/fcntl.c
2082--- linux-2.4.21/fs/fcntl.c 2003-06-23 11:41:29.000000000 -0400
2083+++ linux-2.4.21/fs/fcntl.c 2003-06-23 11:49:16.000000000 -0400
2084@@ -11,6 +11,7 @@
2085 #include <linux/smp_lock.h>
2086 #include <linux/slab.h>
2087 #include <linux/iobuf.h>
2088+#include <linux/grsecurity.h>
2089
2090 #include <asm/poll.h>
2091 #include <asm/siginfo.h>
2092@@ -67,6 +68,7 @@
2093 write_lock(&files->file_lock);
2094
2095 error = -EINVAL;
2096+ gr_learn_resource(current, RLIMIT_NOFILE, orig_start);
2097 if (orig_start >= current->rlim[RLIMIT_NOFILE].rlim_cur)
2098 goto out;
2099
2100@@ -86,6 +88,7 @@
2101 }
2102
2103 error = -EMFILE;
2104+ gr_learn_resource(current, RLIMIT_NOFILE, newfd);
2105 if (newfd >= current->rlim[RLIMIT_NOFILE].rlim_cur)
2106 goto out;
2107
2108@@ -148,6 +151,7 @@
2109 if (newfd == oldfd)
2110 goto out_unlock;
2111 err = -EBADF;
2112+ gr_learn_resource(current, RLIMIT_NOFILE, newfd);
2113 if (newfd >= current->rlim[RLIMIT_NOFILE].rlim_cur)
2114 goto out_unlock;
2115 get_file(file); /* We are now finished with oldfd */
2116@@ -448,6 +452,10 @@
2117 match = -p->pgrp;
2118 if (pid != match)
2119 continue;
2120+ if (gr_check_protected_task(p))
2121+ continue;
2122+ if (gr_pid_is_chrooted(p))
2123+ continue;
2124 send_sigio_to_task(p, fown, fd, band);
2125 }
2126 out:
2127diff -urN linux-2.4.21/fs/locks.c linux-2.4.21/fs/locks.c
2128--- linux-2.4.21/fs/locks.c 2003-06-23 11:41:28.000000000 -0400
2129+++ linux-2.4.21/fs/locks.c 2003-06-23 11:49:16.000000000 -0400
2130@@ -138,6 +138,7 @@
2131 static struct file_lock *locks_alloc_lock(int account)
2132 {
2133 struct file_lock *fl;
2134+ if(account) gr_learn_resource(current, RLIMIT_LOCKS, current->locks);
2135 if (account && current->locks >= current->rlim[RLIMIT_LOCKS].rlim_cur)
2136 return NULL;
2137 fl = kmem_cache_alloc(filelock_cache, SLAB_KERNEL);
2138diff -urN linux-2.4.21/fs/namei.c linux-2.4.21/fs/namei.c
2139--- linux-2.4.21/fs/namei.c 2003-06-23 11:41:28.000000000 -0400
2140+++ linux-2.4.21/fs/namei.c 2003-06-23 11:49:16.000000000 -0400
2141@@ -22,6 +22,7 @@
2142 #include <linux/dnotify.h>
2143 #include <linux/smp_lock.h>
2144 #include <linux/personality.h>
2145+#include <linux/grsecurity.h>
2146
2147 #include <asm/namei.h>
2148 #include <asm/uaccess.h>
2149@@ -343,6 +344,13 @@
2150 current->state = TASK_RUNNING;
2151 schedule();
2152 }
2153+
2154+ if (gr_handle_follow_link(dentry->d_parent->d_inode,
2155+ dentry->d_inode, dentry, nd->mnt)) {
2156+ path_release(nd);
2157+ return -EACCES;
2158+ }
2159+
2160 current->link_count++;
2161 current->total_link_count++;
2162 UPDATE_ATIME(dentry->d_inode);
2163@@ -641,6 +649,10 @@
2164 }
2165 }
2166 return_base:
2167+ if (!gr_acl_handle_hidden_file(nd->dentry, nd->mnt)) {
2168+ path_release(nd);
2169+ return -ENOENT;
2170+ }
2171 return 0;
2172 out_dput:
2173 dput(dentry);
2174@@ -1013,7 +1025,21 @@
2175 error = path_lookup(pathname, lookup_flags(flag), nd);
2176 if (error)
2177 return error;
2178+
2179+ if (gr_acl_is_enabled() && nd->dentry->d_inode &&
2180+ S_ISBLK(nd->dentry->d_inode->i_mode) &&
2181+ !capable(CAP_SYS_RAWIO)) {
2182+ error = -EPERM;
2183+ goto exit;
2184+ }
2185+
2186+ if (!gr_acl_handle_open(nd->dentry, nd->mnt, flag)) {
2187+ error = -EACCES;
2188+ goto exit;
2189+ }
2190+
2191 dentry = nd->dentry;
2192+
2193 goto ok;
2194 }
2195
2196@@ -1046,7 +1072,16 @@
2197 if (!dentry->d_inode) {
2198 if (!IS_POSIXACL(dir->d_inode))
2199 mode &= ~current->fs->umask;
2200+ if (!gr_acl_handle_creat(dentry, nd->dentry, nd->mnt, flag, mode)) {
2201+ error = -EACCES;
2202+ up(&dir->d_inode->i_sem);
2203+ goto exit_dput;
2204+ }
2205+
2206 error = vfs_create(dir->d_inode, dentry, mode);
2207+ if (!error)
2208+ gr_handle_create(dentry, nd->mnt);
2209+
2210 up(&dir->d_inode->i_sem);
2211 dput(nd->dentry);
2212 nd->dentry = dentry;
2213@@ -1056,12 +1091,35 @@
2214 /* Don't check for write permission, don't truncate */
2215 acc_mode = 0;
2216 flag &= ~O_TRUNC;
2217+
2218 goto ok;
2219 }
2220
2221 /*
2222 * It already exists.
2223 */
2224+
2225+ if (gr_acl_is_enabled() && S_ISBLK(dentry->d_inode->i_mode) &&
2226+ !capable(CAP_SYS_RAWIO)) {
2227+ error = -EPERM;
2228+ up(&dir->d_inode->i_sem);
2229+ goto exit_dput;
2230+ }
2231+
2232+ if (!gr_acl_handle_open(dentry, nd->mnt, flag)) {
2233+ error = -EACCES;
2234+ up(&dir->d_inode->i_sem);
2235+ goto exit_dput;
2236+ }
2237+
2238+ inode = dentry->d_inode;
2239+
2240+ if (gr_handle_fifo(dentry, nd->mnt, dir, flag, acc_mode)) {
2241+ up(&dir->d_inode->i_sem);
2242+ error = -EACCES;
2243+ goto exit_dput;
2244+ }
2245+
2246 up(&dir->d_inode->i_sem);
2247
2248 error = -EEXIST;
2249@@ -1151,7 +1209,7 @@
2250 if (!error) {
2251 DQUOT_INIT(inode);
2252
2253- error = do_truncate(dentry, 0);
2254+ error = do_truncate(dentry,0,nd->mnt);
2255 }
2256 put_write_access(inode);
2257 if (error)
2258@@ -1182,6 +1240,13 @@
2259 * stored in nd->last.name and we will have to putname() it when we
2260 * are done. Procfs-like symlinks just set LAST_BIND.
2261 */
2262+
2263+ if (gr_handle_follow_link(dentry->d_parent->d_inode, dentry->d_inode,
2264+ dentry, nd->mnt)) {
2265+ error = -EACCES;
2266+ goto exit_dput;
2267+ }
2268+
2269 UPDATE_ATIME(dentry->d_inode);
2270 error = dentry->d_inode->i_op->follow_link(dentry, nd);
2271 dput(dentry);
2272@@ -1280,6 +1345,18 @@
2273
2274 mode &= ~current->fs->umask;
2275 if (!IS_ERR(dentry)) {
2276+ if (gr_handle_chroot_mknod(dentry, nd.mnt, mode)) {
2277+ error = -EPERM;
2278+ dput(dentry);
2279+ goto out_dput;
2280+ }
2281+
2282+ if (!gr_acl_handle_mknod(dentry, nd.dentry, nd.mnt, mode)) {
2283+ error = -EACCES;
2284+ dput(dentry);
2285+ goto out_dput;
2286+ }
2287+
2288 switch (mode & S_IFMT) {
2289 case 0: case S_IFREG:
2290 error = vfs_create(nd.dentry->d_inode,dentry,mode);
2291@@ -1293,8 +1370,13 @@
2292 default:
2293 error = -EINVAL;
2294 }
2295+
2296+ if(!error)
2297+ gr_handle_create(dentry, nd.mnt);
2298+
2299 dput(dentry);
2300 }
2301+out_dput:
2302 up(&nd.dentry->d_inode->i_sem);
2303 path_release(&nd);
2304 out:
2305@@ -1346,7 +1428,16 @@
2306 if (!IS_ERR(dentry)) {
2307 if (!IS_POSIXACL(nd.dentry->d_inode))
2308 mode &= ~current->fs->umask;
2309- error = vfs_mkdir(nd.dentry->d_inode, dentry, mode);
2310+ error = 0;
2311+
2312+ if (!gr_acl_handle_mkdir(dentry, nd.dentry, nd.mnt))
2313+ error = -EACCES;
2314+
2315+ if(!error)
2316+ error = vfs_mkdir(nd.dentry->d_inode, dentry, mode);
2317+ if(!error)
2318+ gr_handle_create(dentry, nd.mnt);
2319+
2320 dput(dentry);
2321 }
2322 up(&nd.dentry->d_inode->i_sem);
2323@@ -1431,6 +1522,8 @@
2324 char * name;
2325 struct dentry *dentry;
2326 struct nameidata nd;
2327+ ino_t saved_ino = 0;
2328+ kdev_t saved_dev = 0;
2329
2330 name = getname(pathname);
2331 if(IS_ERR(name))
2332@@ -1455,7 +1548,22 @@
2333 dentry = lookup_hash(&nd.last, nd.dentry);
2334 error = PTR_ERR(dentry);
2335 if (!IS_ERR(dentry)) {
2336- error = vfs_rmdir(nd.dentry->d_inode, dentry);
2337+ error = 0;
2338+ if (dentry->d_inode) {
2339+ if (dentry->d_inode->i_nlink <= 1) {
2340+ saved_ino = dentry->d_inode->i_ino;
2341+ saved_dev = dentry->d_inode->i_dev;
2342+ }
2343+
2344+ if (!gr_acl_handle_rmdir(dentry, nd.mnt))
2345+ error = -EACCES;
2346+ }
2347+
2348+ if (!error)
2349+ error = vfs_rmdir(nd.dentry->d_inode, dentry);
2350+ if (!error && (saved_dev || saved_ino))
2351+ gr_handle_delete(saved_ino,saved_dev);
2352+
2353 dput(dentry);
2354 }
2355 up(&nd.dentry->d_inode->i_sem);
2356@@ -1499,6 +1607,8 @@
2357 char * name;
2358 struct dentry *dentry;
2359 struct nameidata nd;
2360+ ino_t saved_ino = 0;
2361+ kdev_t saved_dev = 0;
2362
2363 name = getname(pathname);
2364 if(IS_ERR(name))
2365@@ -1517,7 +1627,21 @@
2366 /* Why not before? Because we want correct error value */
2367 if (nd.last.name[nd.last.len])
2368 goto slashes;
2369- error = vfs_unlink(nd.dentry->d_inode, dentry);
2370+ error = 0;
2371+ if (dentry->d_inode) {
2372+ if (dentry->d_inode->i_nlink <= 1) {
2373+ saved_ino = dentry->d_inode->i_ino;
2374+ saved_dev = dentry->d_inode->i_dev;
2375+ }
2376+
2377+ if (!gr_acl_handle_unlink(dentry, nd.mnt))
2378+ error = -EACCES;
2379+ }
2380+
2381+ if (!error)
2382+ error = vfs_unlink(nd.dentry->d_inode, dentry);
2383+ if (!error && (saved_ino || saved_dev))
2384+ gr_handle_delete(saved_ino,saved_dev);
2385 exit2:
2386 dput(dentry);
2387 }
2388@@ -1581,7 +1705,15 @@
2389 dentry = lookup_create(&nd, 0);
2390 error = PTR_ERR(dentry);
2391 if (!IS_ERR(dentry)) {
2392- error = vfs_symlink(nd.dentry->d_inode, dentry, from);
2393+ error = 0;
2394+
2395+ if (!gr_acl_handle_symlink(dentry, nd.dentry, nd.mnt, from))
2396+ error = -EACCES;
2397+
2398+ if(!error)
2399+ error = vfs_symlink(nd.dentry->d_inode, dentry, from);
2400+ if (!error)
2401+ gr_handle_create(dentry, nd.mnt);
2402 dput(dentry);
2403 }
2404 up(&nd.dentry->d_inode->i_sem);
2405@@ -1665,7 +1797,27 @@
2406 new_dentry = lookup_create(&nd, 0);
2407 error = PTR_ERR(new_dentry);
2408 if (!IS_ERR(new_dentry)) {
2409- error = vfs_link(old_nd.dentry, nd.dentry->d_inode, new_dentry);
2410+ error = 0;
2411+
2412+ if (gr_handle_hardlink(old_nd.dentry, old_nd.mnt,
2413+ old_nd.dentry->d_inode,
2414+ old_nd.dentry->d_inode->i_mode, to)) {
2415+ error = -EPERM;
2416+ goto out_error;
2417+ }
2418+
2419+ if (!gr_acl_handle_link(new_dentry, nd.dentry, nd.mnt,
2420+ old_nd.dentry, old_nd.mnt, to)) {
2421+ error = -EACCES;
2422+ goto out_error;
2423+ }
2424+
2425+ error = vfs_link(old_nd.dentry,
2426+ nd.dentry->d_inode, new_dentry);
2427+
2428+ if (!error)
2429+ gr_handle_create(new_dentry, nd.mnt);
2430+out_error:
2431 dput(new_dentry);
2432 }
2433 up(&nd.dentry->d_inode->i_sem);
2434@@ -1896,10 +2048,15 @@
2435 if (IS_ERR(new_dentry))
2436 goto exit4;
2437
2438- lock_kernel();
2439- error = vfs_rename(old_dir->d_inode, old_dentry,
2440+ error = gr_acl_handle_rename(new_dentry, newnd.dentry, newnd.mnt,
2441+ old_dentry, old_dir->d_inode, oldnd.mnt, newname);
2442+
2443+ if (error == 1) {
2444+ lock_kernel();
2445+ error = vfs_rename(old_dir->d_inode, old_dentry,
2446 new_dir->d_inode, new_dentry);
2447- unlock_kernel();
2448+ unlock_kernel();
2449+ }
2450
2451 dput(new_dentry);
2452 exit4:
2453diff -urN linux-2.4.21/fs/namespace.c linux-2.4.21/fs/namespace.c
2454--- linux-2.4.21/fs/namespace.c 2003-06-23 11:41:28.000000000 -0400
2455+++ linux-2.4.21/fs/namespace.c 2003-06-23 11:49:16.000000000 -0400
2456@@ -15,6 +15,8 @@
2457 #include <linux/quotaops.h>
2458 #include <linux/acct.h>
2459 #include <linux/module.h>
2460+#include <linux/sched.h>
2461+#include <linux/grsecurity.h>
2462
2463 #include <asm/uaccess.h>
2464
2465@@ -325,6 +327,8 @@
2466 lock_kernel();
2467 retval = do_remount_sb(sb, MS_RDONLY, 0);
2468 unlock_kernel();
2469+
2470+ gr_log_remount(mnt->mnt_devname, retval);
2471 }
2472 up_write(&sb->s_umount);
2473 return retval;
2474@@ -350,6 +354,9 @@
2475 }
2476 spin_unlock(&dcache_lock);
2477 up_write(&current->namespace->sem);
2478+
2479+ gr_log_unmount(mnt->mnt_devname, retval);
2480+
2481 return retval;
2482 }
2483
2484@@ -729,6 +736,12 @@
2485 if (retval)
2486 return retval;
2487
2488+ if (gr_handle_chroot_mount(nd.dentry, nd.mnt, dev_name)) {
2489+ retval = -EPERM;
2490+ path_release(&nd);
2491+ return retval;
2492+ }
2493+
2494 if (flags & MS_REMOUNT)
2495 retval = do_remount(&nd, flags & ~MS_REMOUNT, mnt_flags,
2496 data_page);
2497@@ -740,6 +753,9 @@
2498 retval = do_add_mount(&nd, type_page, flags, mnt_flags,
2499 dev_name, data_page);
2500 path_release(&nd);
2501+
2502+ gr_log_mount(dev_name, dir_name, retval);
2503+
2504 return retval;
2505 }
2506
2507@@ -909,6 +925,9 @@
2508 if (!capable(CAP_SYS_ADMIN))
2509 return -EPERM;
2510
2511+ if (gr_handle_chroot_pivot())
2512+ return -EPERM;
2513+
2514 lock_kernel();
2515
2516 error = __user_walk(new_root, LOOKUP_POSITIVE|LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &new_nd);
2517diff -urN linux-2.4.21/fs/open.c linux-2.4.21/fs/open.c
2518--- linux-2.4.21/fs/open.c 2003-06-23 11:41:28.000000000 -0400
2519+++ linux-2.4.21/fs/open.c 2003-06-23 11:49:16.000000000 -0400
2520@@ -15,6 +15,7 @@
2521 #include <linux/slab.h>
2522 #include <linux/tty.h>
2523 #include <linux/iobuf.h>
2524+#include <linux/grsecurity.h>
2525
2526 #include <asm/uaccess.h>
2527
2528@@ -95,7 +96,7 @@
2529 write_unlock(&files->file_lock);
2530 }
2531
2532-int do_truncate(struct dentry *dentry, loff_t length)
2533+int do_truncate(struct dentry *dentry, loff_t length, struct vfsmount *mnt)
2534 {
2535 struct inode *inode = dentry->d_inode;
2536 int error;
2537@@ -105,6 +106,9 @@
2538 if (length < 0)
2539 return -EINVAL;
2540
2541+ if (!gr_acl_handle_truncate(dentry, mnt))
2542+ return -EACCES;
2543+
2544 down(&inode->i_sem);
2545 newattrs.ia_size = length;
2546 newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
2547@@ -163,7 +167,7 @@
2548 error = locks_verify_truncate(inode, NULL, length);
2549 if (!error) {
2550 DQUOT_INIT(inode);
2551- error = do_truncate(nd.dentry, length);
2552+ error = do_truncate(nd.dentry, length, nd.mnt);
2553 }
2554 put_write_access(inode);
2555
2556@@ -215,7 +219,7 @@
2557
2558 error = locks_verify_truncate(inode, file, length);
2559 if (!error)
2560- error = do_truncate(dentry, length);
2561+ error = do_truncate(dentry, length, file->f_vfsmnt);
2562 out_putf:
2563 fput(file);
2564 out:
2565@@ -284,6 +288,12 @@
2566 (error = permission(inode,MAY_WRITE)) != 0)
2567 goto dput_and_out;
2568 }
2569+
2570+ if (!gr_acl_handle_utime(nd.dentry, nd.mnt)) {
2571+ error = -EACCES;
2572+ goto dput_and_out;
2573+ }
2574+
2575 error = notify_change(nd.dentry, &newattrs);
2576 dput_and_out:
2577 path_release(&nd);
2578@@ -329,6 +339,12 @@
2579 (error = permission(inode,MAY_WRITE)) != 0)
2580 goto dput_and_out;
2581 }
2582+
2583+ if (!gr_acl_handle_utime(nd.dentry, nd.mnt)) {
2584+ error = -EACCES;
2585+ goto dput_and_out;
2586+ }
2587+
2588 error = notify_change(nd.dentry, &newattrs);
2589 dput_and_out:
2590 path_release(&nd);
2591@@ -371,6 +387,10 @@
2592 if(!res && (mode & S_IWOTH) && IS_RDONLY(nd.dentry->d_inode)
2593 && !special_file(nd.dentry->d_inode->i_mode))
2594 res = -EROFS;
2595+
2596+ if (!res && !gr_acl_handle_access(nd.dentry, nd.mnt, mode))
2597+ res = -EACCES;
2598+
2599 path_release(&nd);
2600 }
2601
2602@@ -394,6 +414,8 @@
2603 if (error)
2604 goto dput_and_out;
2605
2606+ gr_log_chdir(nd.dentry, nd.mnt);
2607+
2608 set_fs_pwd(current->fs, nd.mnt, nd.dentry);
2609
2610 dput_and_out:
2611@@ -424,6 +446,13 @@
2612 goto out_putf;
2613
2614 error = permission(inode, MAY_EXEC);
2615+
2616+ if (!error && !gr_chroot_fchdir(dentry, mnt))
2617+ error = -EPERM;
2618+
2619+ if (!error)
2620+ gr_log_chdir(dentry, mnt);
2621+
2622 if (!error)
2623 set_fs_pwd(current->fs, mnt, dentry);
2624 out_putf:
2625@@ -450,8 +479,16 @@
2626 if (!capable(CAP_SYS_CHROOT))
2627 goto dput_and_out;
2628
2629+ if (gr_handle_chroot_chroot(nd.dentry, nd.mnt))
2630+ goto dput_and_out;
2631+
2632 set_fs_root(current->fs, nd.mnt, nd.dentry);
2633 set_fs_altroot();
2634+
2635+ gr_handle_chroot_caps(current);
2636+
2637+ gr_handle_chroot_chdir(nd.dentry, nd.mnt);
2638+
2639 error = 0;
2640 dput_and_out:
2641 path_release(&nd);
2642@@ -480,8 +517,20 @@
2643 err = -EPERM;
2644 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
2645 goto out_putf;
2646+
2647+ if (!gr_acl_handle_fchmod(dentry, file->f_vfsmnt, mode)) {
2648+ err = -EACCES;
2649+ goto out_putf;
2650+ }
2651+
2652 if (mode == (mode_t) -1)
2653 mode = inode->i_mode;
2654+
2655+ if (gr_handle_chroot_chmod(dentry, file->f_vfsmnt, mode)) {
2656+ err = -EPERM;
2657+ goto out_putf;
2658+ }
2659+
2660 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
2661 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
2662 err = notify_change(dentry, &newattrs);
2663@@ -512,8 +561,19 @@
2664 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
2665 goto dput_and_out;
2666
2667+ if (!gr_acl_handle_chmod(nd.dentry, nd.mnt, mode)) {
2668+ error = -EACCES;
2669+ goto dput_and_out;
2670+ }
2671+
2672 if (mode == (mode_t) -1)
2673 mode = inode->i_mode;
2674+
2675+ if (gr_handle_chroot_chmod(nd.dentry, nd.mnt, mode)) {
2676+ error = -EACCES;
2677+ goto dput_and_out;
2678+ }
2679+
2680 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
2681 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
2682 error = notify_change(nd.dentry, &newattrs);
2683@@ -524,7 +584,7 @@
2684 return error;
2685 }
2686
2687-static int chown_common(struct dentry * dentry, uid_t user, gid_t group)
2688+static int chown_common(struct dentry * dentry, uid_t user, gid_t group, struct vfsmount *mnt)
2689 {
2690 struct inode * inode;
2691 int error;
2692@@ -541,6 +601,12 @@
2693 error = -EPERM;
2694 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
2695 goto out;
2696+
2697+ if (!gr_acl_handle_chown(dentry, mnt)) {
2698+ error = -EACCES;
2699+ goto out;
2700+ }
2701+
2702 if (user == (uid_t) -1)
2703 user = inode->i_uid;
2704 if (group == (gid_t) -1)
2705@@ -591,7 +657,7 @@
2706
2707 error = user_path_walk(filename, &nd);
2708 if (!error) {
2709- error = chown_common(nd.dentry, user, group);
2710+ error = chown_common(nd.dentry, user, group, nd.mnt);
2711 path_release(&nd);
2712 }
2713 return error;
2714@@ -604,7 +670,7 @@
2715
2716 error = user_path_walk_link(filename, &nd);
2717 if (!error) {
2718- error = chown_common(nd.dentry, user, group);
2719+ error = chown_common(nd.dentry, user, group, nd.mnt);
2720 path_release(&nd);
2721 }
2722 return error;
2723@@ -618,7 +684,8 @@
2724
2725 file = fget(fd);
2726 if (file) {
2727- error = chown_common(file->f_dentry, user, group);
2728+ error = chown_common(file->f_dentry, user,
2729+ group, file->f_vfsmnt);
2730 fput(file);
2731 }
2732 return error;
2733@@ -738,6 +805,7 @@
2734 * N.B. For clone tasks sharing a files structure, this test
2735 * will limit the total number of files that can be opened.
2736 */
2737+ gr_learn_resource(current, RLIMIT_NOFILE, fd);
2738 if (fd >= current->rlim[RLIMIT_NOFILE].rlim_cur)
2739 goto out;
2740
2741diff -urN linux-2.4.21/fs/proc/array.c linux-2.4.21/fs/proc/array.c
2742--- linux-2.4.21/fs/proc/array.c 2003-06-23 11:41:29.000000000 -0400
2743+++ linux-2.4.21/fs/proc/array.c 2003-06-23 11:49:16.000000000 -0400
2744@@ -334,6 +340,11 @@
2745
2746 wchan = get_wchan(task);
2747
2748+#ifdef CONFIG_GRKERNSEC_HIDESYM
2749+ eip = 0;
2750+ esp = 0;
2751+#endif
2752+
2753 collect_sigign_sigcatch(task, &sigign, &sigcatch);
2754
2755 /* scale priority and nice values from timeslices to -20..20 */
2756@@ -684,6 +727,16 @@
2757 return retval;
2758 }
2759
2760+#ifdef CONFIG_GRKERNSEC_PROC_IPADDR
2761+int proc_pid_ipaddr(struct task_struct *task, char * buffer)
2762+{
2763+ int len;
2764+
2765+ len = sprintf(buffer, "%u.%u.%u.%u\n", NIPQUAD(task->curr_ip));
2766+ return len;
2767+}
2768+#endif
2769+
2770 #ifdef CONFIG_SMP
2771 int proc_pid_cpu(struct task_struct *task, char * buffer)
2772 {
2773diff -urN linux-2.4.21/fs/proc/base.c linux-2.4.21/fs/proc/base.c
2774--- linux-2.4.21/fs/proc/base.c 2003-06-23 11:41:29.000000000 -0400
2775+++ linux-2.4.21/fs/proc/base.c 2003-06-23 11:49:16.000000000 -0400
2776@@ -25,6 +25,7 @@
2777 #include <linux/string.h>
2778 #include <linux/seq_file.h>
2779 #include <linux/namespace.h>
2780+#include <linux/grsecurity.h>
2781
2782 /*
2783 * For hysterical raisins we keep the same inumbers as in the old procfs.
2784@@ -41,6 +42,9 @@
2785 int proc_pid_status(struct task_struct*,char*);
2786 int proc_pid_statm(struct task_struct*,char*);
2787 int proc_pid_cpu(struct task_struct*,char*);
2788+#ifdef CONFIG_GRKERNSEC_PROC_IPADDR
2789+int proc_pid_ipaddr(struct task_struct*,char*);
2790+#endif
2791
2792 static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
2793 {
2794@@ -228,9 +232,22 @@
2795
2796 static int proc_permission(struct inode *inode, int mask)
2797 {
2798+ int ret;
2799+ struct task_struct *task;
2800+
2801 if (vfs_permission(inode, mask) != 0)
2802 return -EACCES;
2803- return proc_check_root(inode);
2804+ ret = proc_check_root(inode);
2805+
2806+ if (ret)
2807+ return ret;
2808+
2809+ task = inode->u.proc_i.task;
2810+
2811+ if (!task)
2812+ return 0;
2813+
2814+ return gr_acl_handle_procpidmem(task);
2815 }
2816
2817 static ssize_t pid_maps_read(struct file * file, char * buf,
2818@@ -546,6 +563,9 @@
2819 PROC_PID_STATM,
2820 PROC_PID_MAPS,
2821 PROC_PID_CPU,
2822+#ifdef CONFIG_GRKERNSEC_PROC_IPADDR
2823+ PROC_PID_IPADDR,
2824+#endif
2825 PROC_PID_MOUNTS,
2826 PROC_PID_FD_DIR = 0x8000, /* 0x8000-0xffff */
2827 };
2828@@ -561,6 +581,9 @@
2829 #ifdef CONFIG_SMP
2830 E(PROC_PID_CPU, "cpu", S_IFREG|S_IRUGO),
2831 #endif
2832+#ifdef CONFIG_GRKERNSEC_PROC_IPADDR
2833+ E(PROC_PID_IPADDR, "ipaddr", S_IFREG|S_IRUSR),
2834+#endif
2835 E(PROC_PID_MAPS, "maps", S_IFREG|S_IRUGO),
2836 E(PROC_PID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
2837 E(PROC_PID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
2838@@ -717,10 +740,17 @@
2839 get_task_struct(task);
2840 inode->u.proc_i.task = task;
2841 inode->i_uid = 0;
2842+#ifdef CONFIG_GRKERNSEC_PROC_USERGROUP
2843+ inode->i_gid = CONFIG_GRKERNSEC_PROC_GID;
2844+#else
2845 inode->i_gid = 0;
2846+#endif
2847+
2848 if (ino == PROC_PID_INO || task_dumpable(task)) {
2849 inode->i_uid = task->euid;
2850+#ifndef CONFIG_GRKERNSEC_PROC_USERGROUP
2851 inode->i_gid = task->egid;
2852+#endif
2853 }
2854
2855 out:
2856@@ -928,6 +958,12 @@
2857 inode->u.proc_i.op.proc_read = proc_pid_cpu;
2858 break;
2859 #endif
2860+#ifdef CONFIG_GRKERNSEC_PROC_IPADDR
2861+ case PROC_PID_IPADDR:
2862+ inode->i_fop = &proc_info_file_operations;
2863+ inode->u.proc_i.op.proc_read = proc_pid_ipaddr;
2864+ break;
2865+#endif
2866 case PROC_PID_MEM:
2867 inode->i_op = &proc_mem_inode_operations;
2868 inode->i_fop = &proc_mem_operations;
2869@@ -1026,13 +1062,34 @@
2870 if (!task)
2871 goto out;
2872
2873+ if(gr_check_hidden_task(task)) {
2874+ free_task_struct(task);
2875+ goto out;
2876+ }
2877+
2878+#ifdef CONFIG_GRKERNSEC_PROC
2879+ if (current->uid && (task->uid != current->uid)
2880+#ifdef CONFIG_GRKERNSEC_PROC_USERGROUP
2881+ && !in_group_p(CONFIG_GRKERNSEC_PROC_GID)
2882+#endif
2883+ ) {
2884+ free_task_struct(task);
2885+ goto out;
2886+ }
2887+#endif
2888 inode = proc_pid_make_inode(dir->i_sb, task, PROC_PID_INO);
2889
2890 free_task_struct(task);
2891
2892 if (!inode)
2893 goto out;
2894+#ifdef CONFIG_GRKERNSEC_PROC_USER
2895+ inode->i_mode = S_IFDIR|S_IRUSR|S_IXUSR;
2896+#elif CONFIG_GRKERNSEC_PROC_USERGROUP
2897+ inode->i_mode = S_IFDIR|S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP;
2898+#else
2899 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2900+#endif
2901 inode->i_op = &proc_base_inode_operations;
2902 inode->i_fop = &proc_base_operations;
2903 inode->i_nlink = 3;
2904@@ -1072,6 +1129,18 @@
2905 int pid = p->pid;
2906 if (!pid)
2907 continue;
2908+ if(gr_pid_is_chrooted(p))
2909+ continue;
2910+ if(gr_check_hidden_task(p))
2911+ continue;
2912+#ifdef CONFIG_GRKERNSEC_PROC
2913+ if (current->uid && (p->uid != current->uid)
2914+#ifdef CONFIG_GRKERNSEC_PROC_USERGROUP
2915+ && !in_group_p(CONFIG_GRKERNSEC_PROC_GID)
2916+#endif
2917+ )
2918+ continue;
2919+#endif
2920 if (--index >= 0)
2921 continue;
2922 pids[nr_pids] = pid;
2923diff -urN linux-2.4.21/fs/proc/generic.c linux-2.4.21/fs/proc/generic.c
2924--- linux-2.4.21/fs/proc/generic.c 2003-06-23 11:41:29.000000000 -0400
2925+++ linux-2.4.21/fs/proc/generic.c 2003-06-23 11:49:16.000000000 -0400
2926@@ -503,6 +503,29 @@
2927 return ent;
2928 }
2929
2930+#ifdef CONFIG_GRKERNSEC_PROC
2931+struct proc_dir_entry *proc_priv_mkdir(const char *name, struct proc_dir_entry *parent)
2932+{
2933+ struct proc_dir_entry *ent;
2934+ mode_t mode = 0;
2935+
2936+#ifdef CONFIG_GRKERNSEC_PROC_USER
2937+ mode = S_IFDIR | S_IRUSR | S_IXUSR;
2938+#elif CONFIG_GRKERNSEC_PROC_USERGROUP
2939+ mode = S_IFDIR | S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP;
2940+#endif
2941+
2942+ ent = proc_create(&parent, name, mode, 2);
2943+ if (ent) {
2944+ ent->proc_fops = &proc_dir_operations;
2945+ ent->proc_iops = &proc_dir_inode_operations;
2946+
2947+ proc_register(parent, ent);
2948+ }
2949+ return ent;
2950+}
2951+#endif
2952+
2953 struct proc_dir_entry *create_proc_entry(const char *name, mode_t mode,
2954 struct proc_dir_entry *parent)
2955 {
2956diff -urN linux-2.4.21/fs/proc/inode.c linux-2.4.21/fs/proc/inode.c
2957--- linux-2.4.21/fs/proc/inode.c 2003-06-23 11:41:29.000000000 -0400
2958+++ linux-2.4.21/fs/proc/inode.c 2003-06-23 11:49:16.000000000 -0400
2959@@ -152,7 +152,11 @@
2960 if (de->mode) {
2961 inode->i_mode = de->mode;
2962 inode->i_uid = de->uid;
2963+#ifdef CONFIG_GRKERNSEC_PROC_USERGROUP
2964+ inode->i_gid = CONFIG_GRKERNSEC_PROC_GID;
2965+#else
2966 inode->i_gid = de->gid;
2967+#endif
2968 }
2969 if (de->size)
2970 inode->i_size = de->size;
2971diff -urN linux-2.4.21/fs/proc/proc_misc.c linux-2.4.21/fs/proc/proc_misc.c
2972--- linux-2.4.21/fs/proc/proc_misc.c 2003-06-23 11:41:29.000000000 -0400
2973+++ linux-2.4.21/fs/proc/proc_misc.c 2003-06-23 11:49:16.000000000 -0400
2974@@ -568,6 +568,7 @@
2975 void __init proc_misc_init(void)
2976 {
2977 struct proc_dir_entry *entry;
2978+ int gr_mode = 0;
2979 static struct {
2980 char *name;
2981 int (*read_proc)(char*,char**,off_t,int,int*,void*);
2982@@ -582,48 +583,81 @@
2983 #ifdef CONFIG_STRAM_PROC
2984 {"stram", stram_read_proc},
2985 #endif
2986-#ifdef CONFIG_MODULES
2987+#if defined(CONFIG_MODULES) && !defined(CONFIG_GRKERNSEC_PROC)
2988 {"modules", modules_read_proc},
2989 #endif
2990 {"stat", kstat_read_proc},
2991+#ifndef CONFIG_GRKERNSEC_PROC_ADD
2992 {"devices", devices_read_proc},
2993-#if !defined(CONFIG_ARCH_S390)
2994+#endif
2995+#if !defined(CONFIG_ARCH_S390) && !defined(CONFIG_GRKERNSEC_PROC_ADD)
2996 {"interrupts", interrupts_read_proc},
2997 #endif
2998 {"filesystems", filesystems_read_proc},
2999+#ifndef CONFIG_GRKERNSEC_PROC_ADD
3000 {"dma", dma_read_proc},
3001 {"ioports", ioports_read_proc},
3002 {"cmdline", cmdline_read_proc},
3003+#endif
3004 #ifdef CONFIG_SGI_DS1286
3005 {"rtc", ds1286_read_proc},
3006 #endif
3007 {"locks", locks_read_proc},
3008 {"swaps", swaps_read_proc},
3009+#ifndef CONFIG_GRKERNSEC_PROC_ADD
3010 {"iomem", memory_read_proc},
3011+#endif
3012 {"execdomains", execdomains_read_proc},
3013 {NULL,}
3014 };
3015 for (p = simple_ones; p->name; p++)
3016 create_proc_read_entry(p->name, 0, NULL, p->read_proc, NULL);
3017
3018+#ifdef CONFIG_GRKERNSEC_PROC_USER
3019+ gr_mode = S_IRUSR;
3020+#elif CONFIG_GRKERNSEC_PROC_USERGROUP
3021+ gr_mode = S_IRUSR | S_IRGRP;
3022+#endif
3023+
3024+#if defined(CONFIG_GRKERNSEC_PROC) && defined(CONFIG_MODULES)
3025+ create_proc_read_entry("modules", gr_mode, NULL, &modules_read_proc, NULL);
3026+#endif
3027+#ifdef CONFIG_GRKERNSEC_PROC_ADD
3028+ create_proc_read_entry("devices", gr_mode, NULL, &devices_read_proc, NULL);
3029+ create_proc_read_entry("dma", gr_mode, NULL, &dma_read_proc, NULL);
3030+ create_proc_read_entry("ioports", gr_mode, NULL, &ioports_read_proc, NULL);
3031+ create_proc_read_entry("cmdline", gr_mode, NULL, &cmdline_read_proc, NULL);
3032+ create_proc_read_entry("iomem", gr_mode, NULL, &memory_read_proc, NULL);
3033+#if !defined(CONFIG_ARCH_S390)
3034+ create_proc_read_entry("interrupts", gr_mode, NULL, &interrupts_read_proc, NULL);
3035+#endif
3036+#endif
3037+
3038 proc_symlink("mounts", NULL, "self/mounts");
3039
3040 /* And now for trickier ones */
3041 entry = create_proc_entry("kmsg", S_IRUSR, &proc_root);
3042 if (entry)
3043 entry->proc_fops = &proc_kmsg_operations;
3044+#ifdef CONFIG_GRKERNSEC_PROC_ADD
3045+ create_seq_entry("cpuinfo", gr_mode, &proc_cpuinfo_operations);
3046+ create_seq_entry("slabinfo", gr_mode,&proc_slabinfo_operations);
3047+#else
3048 create_seq_entry("cpuinfo", 0, &proc_cpuinfo_operations);
3049- create_seq_entry("partitions", 0, &proc_partitions_operations);
3050 create_seq_entry("slabinfo",S_IWUSR|S_IRUGO,&proc_slabinfo_operations);
3051+#endif
3052+ create_seq_entry("partitions", 0, &proc_partitions_operations);
3053 #ifdef CONFIG_MODULES
3054- create_seq_entry("ksyms", 0, &proc_ksyms_operations);
3055+ create_seq_entry("ksyms", gr_mode, &proc_ksyms_operations);
3056 #endif
3057+#ifndef CONFIG_GRKERNSEC_PROC_ADD
3058 proc_root_kcore = create_proc_entry("kcore", S_IRUSR, NULL);
3059 if (proc_root_kcore) {
3060 proc_root_kcore->proc_fops = &proc_kcore_operations;
3061 proc_root_kcore->size =
3062 (size_t)high_memory - PAGE_OFFSET + PAGE_SIZE;
3063 }
3064+#endif
3065 if (prof_shift) {
3066 entry = create_proc_entry("profile", S_IWUSR | S_IRUGO, NULL);
3067 if (entry) {
3068diff -urN linux-2.4.21/fs/proc/proc_tty.c linux-2.4.21/fs/proc/proc_tty.c
3069--- linux-2.4.21/fs/proc/proc_tty.c 2003-06-23 11:41:29.000000000 -0400
3070+++ linux-2.4.21/fs/proc/proc_tty.c 2003-06-23 11:49:16.000000000 -0400
3071@@ -174,7 +174,11 @@
79ee5d42
JR
3072 * password lengths and inter-keystroke timings during password
3073 * entry.
3074 */
dd08e044
JR
3075+#ifdef CONFIG_GRKERNSEC_PROC
3076+ proc_tty_driver = proc_priv_mkdir("tty/driver", 0);
3077+#else
79ee5d42 3078 proc_tty_driver = proc_mkdir_mode("tty/driver", S_IRUSR | S_IXUSR, 0);
dd08e044
JR
3079+#endif
3080
3081 create_proc_read_entry("tty/ldiscs", 0, 0, tty_ldiscs_read_proc,NULL);
3082 create_proc_read_entry("tty/drivers", 0, 0, tty_drivers_read_proc,NULL);
3083diff -urN linux-2.4.21/fs/proc/root.c linux-2.4.21/fs/proc/root.c
3084--- linux-2.4.21/fs/proc/root.c 2003-06-23 11:41:29.000000000 -0400
3085+++ linux-2.4.21/fs/proc/root.c 2003-06-23 11:49:16.000000000 -0400
3086@@ -37,13 +37,21 @@
3087 return;
3088 }
3089 proc_misc_init();
3090+#ifdef CONFIG_GRKERNSEC_PROC
3091+ proc_net = proc_priv_mkdir("net", 0);
3092+#else
3093 proc_net = proc_mkdir("net", 0);
3094+#endif
3095 #ifdef CONFIG_SYSVIPC
3096 proc_mkdir("sysvipc", 0);
3097 #endif
3098 #ifdef CONFIG_SYSCTL
3099+#ifdef CONFIG_GRKERNSEC_PROC
3100+ proc_sys_root = proc_priv_mkdir("sys", 0);
3101+#else
3102 proc_sys_root = proc_mkdir("sys", 0);
3103 #endif
3104+#endif
3105 #if defined(CONFIG_BINFMT_MISC) || defined(CONFIG_BINFMT_MISC_MODULE)
3106 proc_mkdir("sys/fs", 0);
3107 proc_mkdir("sys/fs/binfmt_misc", 0);
3108@@ -67,7 +75,12 @@
3109 #ifdef CONFIG_PPC_RTAS
3110 proc_rtas_init();
3111 #endif
3112+
3113+#ifdef CONFIG_GRKERNSEC_PROC_ADD
3114+ proc_bus = proc_priv_mkdir("bus", 0);
3115+#else
3116 proc_bus = proc_mkdir("bus", 0);
3117+#endif
3118 }
3119
3120 static struct dentry *proc_root_lookup(struct inode * dir, struct dentry * dentry)
3121diff -urN linux-2.4.21/fs/readdir.c linux-2.4.21/fs/readdir.c
3122--- linux-2.4.21/fs/readdir.c 2003-06-23 11:41:29.000000000 -0400
3123+++ linux-2.4.21/fs/readdir.c 2003-06-23 11:49:16.000000000 -0400
3124@@ -10,6 +10,7 @@
3125 #include <linux/stat.h>
3126 #include <linux/file.h>
3127 #include <linux/smp_lock.h>
3128+#include <linux/grsecurity.h>
3129
3130 #include <asm/uaccess.h>
3131
3132@@ -181,6 +182,7 @@
3133 struct readdir_callback {
3134 struct old_linux_dirent * dirent;
3135 int count;
3136+ struct nameidata nd;
3137 };
3138
3139 static int fillonedir(void * __buf, const char * name, int namlen, loff_t offset,
3140@@ -191,6 +193,10 @@
3141
3142 if (buf->count)
3143 return -EINVAL;
3144+
3145+ if (!gr_acl_handle_filldir(buf->nd.dentry, buf->nd.mnt, ino))
3146+ return 0;
3147+
3148 buf->count++;
3149 dirent = buf->dirent;
3150 put_user(ino, &dirent->d_ino);
3151@@ -215,6 +221,9 @@
3152 buf.count = 0;
3153 buf.dirent = dirent;
3154
3155+ buf.nd.dentry = file->f_dentry;
3156+ buf.nd.mnt = file->f_vfsmnt;
3157+
3158 error = vfs_readdir(file, fillonedir, &buf);
3159 if (error >= 0)
3160 error = buf.count;
3161@@ -242,6 +251,7 @@
3162 struct linux_dirent * previous;
3163 int count;
3164 int error;
3165+ struct nameidata nd;
3166 };
3167
3168 static int filldir(void * __buf, const char * name, int namlen, loff_t offset,
3169@@ -254,6 +264,10 @@
3170 buf->error = -EINVAL; /* only used if we fail.. */
3171 if (reclen > buf->count)
3172 return -EINVAL;
3173+
3174+ if (!gr_acl_handle_filldir(buf->nd.dentry, buf->nd.mnt, ino))
3175+ return 0;
3176+
3177 dirent = buf->previous;
3178 if (dirent)
3179 put_user(offset, &dirent->d_off);
3180@@ -286,6 +300,9 @@
3181 buf.count = count;
3182 buf.error = 0;
3183
3184+ buf.nd.dentry = file->f_dentry;
3185+ buf.nd.mnt = file->f_vfsmnt;
3186+
3187 error = vfs_readdir(file, filldir, &buf);
3188 if (error < 0)
3189 goto out_putf;
3190@@ -320,6 +337,7 @@
3191 struct linux_dirent64 * previous;
3192 int count;
3193 int error;
3194+ struct nameidata nd;
3195 };
3196
3197 static int filldir64(void * __buf, const char * name, int namlen, loff_t offset,
3198@@ -332,6 +350,10 @@
3199 buf->error = -EINVAL; /* only used if we fail.. */
3200 if (reclen > buf->count)
3201 return -EINVAL;
3202+
3203+ if (!gr_acl_handle_filldir(buf->nd.dentry, buf->nd.mnt, ino))
3204+ return 0;
3205+
3206 dirent = buf->previous;
3207 if (dirent) {
3208 d.d_off = offset;
3209@@ -369,6 +391,9 @@
3210 buf.count = count;
3211 buf.error = 0;
3212
3213+ buf.nd.mnt = file->f_vfsmnt;
3214+ buf.nd.dentry = file->f_dentry;
3215+
3216 error = vfs_readdir(file, filldir64, &buf);
3217 if (error < 0)
3218 goto out_putf;
3219diff -urN linux-2.4.21/grsecurity/Config.in linux-2.4.21/grsecurity/Config.in
3220--- linux-2.4.21/grsecurity/Config.in 1969-12-31 19:00:00.000000000 -0500
3221+++ linux-2.4.21/grsecurity/Config.in 2003-06-23 11:49:16.000000000 -0400
3222@@ -0,0 +1,264 @@
3223+choice 'Security level' \
3224+ "Low CONFIG_GRKERNSEC_LOW \
3225+ Medium CONFIG_GRKERNSEC_MID \
3226+ High CONFIG_GRKERNSEC_HI \
3227+ Customized CONFIG_GRKERNSEC_CUSTOM" Customized
3228+if [ "$CONFIG_GRKERNSEC_LOW" = "y" ]; then
3229+define_bool CONFIG_GRKERNSEC_RANDSRC n
3230+define_bool CONFIG_GRKERNSEC_RANDRPC n
3231+define_bool CONFIG_GRKERNSEC_RANDPING n
3232+define_bool CONFIG_GRKERNSEC_FORKFAIL n
3233+define_bool CONFIG_GRKERNSEC_TIME n
3234+define_bool CONFIG_GRKERNSEC_SIGNAL n
3235+define_bool CONFIG_GRKERNSEC_CHROOT_SHMAT n
3236+define_bool CONFIG_GRKERNSEC_CHROOT_MOUNT n
3237+define_bool CONFIG_GRKERNSEC_CHROOT_FCHDIR n
3238+define_bool CONFIG_GRKERNSEC_CHROOT_DOUBLE n
3239+define_bool CONFIG_GRKERNSEC_CHROOT_PIVOT n
3240+define_bool CONFIG_GRKERNSEC_CHROOT_MKNOD n
3241+define_bool CONFIG_GRKERNSEC_PROC n
3242+define_bool CONFIG_GRKERNSEC_PROC_IPADDR n
3243+define_bool CONFIG_GRKERNSEC_HIDESYM n
3244+define_bool CONFIG_GRKERNSEC_CHROOT_CAPS n
3245+define_bool CONFIG_GRKERNSEC_CHROOT_SYSCTL n
3246+define_bool CONFIG_GRKERNSEC_PROC_USERGROUP n
3247+define_bool CONFIG_GRKERNSEC_KMEM n
3248+define_bool CONFIG_GRKERNSEC_PROC_ADD n
3249+define_bool CONFIG_GRKERNSEC_CHROOT_CHMOD n
3250+define_bool CONFIG_GRKERNSEC_CHROOT_NICE n
3251+define_bool CONFIG_GRKERNSEC_CHROOT_FINDTASK n
3252+if [ "$CONFIG_X86" = "y" ]; then
3253+define_bool CONFIG_GRKERNSEC_IO n
3254+fi
3255+define_bool CONFIG_GRKERNSEC_AUDIT_MOUNT n
3256+define_bool CONFIG_GRKERNSEC_ACL_HIDEKERN n
3257+define_bool CONFIG_GRKERNSEC_RESLOG n
3258+define_int CONFIG_GRKERNSEC_ACL_MAXTRIES 3
3259+define_int CONFIG_GRKERNSEC_ACL_TIMEOUT 30
3260+
3261+define_int CONFIG_GRKERNSEC_FLOODTIME 10
3262+define_int CONFIG_GRKERNSEC_FLOODBURST 4
3263+define_bool CONFIG_GRKERNSEC_LINK y
3264+define_bool CONFIG_GRKERNSEC_FIFO y
3265+define_bool CONFIG_GRKERNSEC_RANDPID y
3266+define_bool CONFIG_GRKERNSEC_EXECVE y
3267+define_bool CONFIG_GRKERNSEC_RANDNET y
3268+define_bool CONFIG_GRKERNSEC_RANDISN n
3269+define_bool CONFIG_GRKERNSEC_DMESG y
3270+define_bool CONFIG_GRKERNSEC_RANDID y
3271+define_bool CONFIG_GRKERNSEC_CHROOT_CHDIR y
3272+fi
3273+if [ "$CONFIG_GRKERNSEC_MID" = "y" ]; then
3274+define_bool CONFIG_GRKERNSEC_KMEM n
3275+define_bool CONFIG_GRKERNSEC_PROC_IPADDR n
3276+define_bool CONFIG_GRKERNSEC_HIDESYM n
3277+define_bool CONFIG_GRKERNSEC_PROC_ADD n
3278+define_bool CONFIG_GRKERNSEC_CHROOT_CHMOD n
3279+define_bool CONFIG_GRKERNSEC_CHROOT_NICE n
3280+define_bool CONFIG_GRKERNSEC_CHROOT_FINDTASK n
3281+if [ "$CONFIG_X86" = "y" ]; then
3282+define_bool CONFIG_GRKERNSEC_IO n
3283+fi
3284+define_bool CONFIG_GRKERNSEC_AUDIT_MOUNT n
3285+define_bool CONFIG_GRKERNSEC_CHROOT_CAPS n
3286+define_bool CONFIG_GRKERNSEC_CHROOT_SYSCTL y
3287+define_bool CONFIG_GRKERNSEC_AUDIT_MOUNT n
3288+define_bool CONFIG_GRKERNSEC_CHROOT_FCHDIR n
3289+define_bool CONFIG_GRKERNSEC_ACL_HIDEKERN n
3290+define_bool CONFIG_GRKERNSEC_RESLOG n
3291+define_int CONFIG_GRKERNSEC_ACL_MAXTRIES 3
3292+define_int CONFIG_GRKERNSEC_ACL_TIMEOUT 30
3293+
3294+define_int CONFIG_GRKERNSEC_FLOODTIME 10
3295+define_int CONFIG_GRKERNSEC_FLOODBURST 4
3296+define_bool CONFIG_GRKERNSEC_LINK y
3297+define_bool CONFIG_GRKERNSEC_FIFO y
3298+define_bool CONFIG_GRKERNSEC_RANDPID y
3299+define_bool CONFIG_GRKERNSEC_EXECVE y
3300+define_bool CONFIG_GRKERNSEC_DMESG y
3301+define_bool CONFIG_GRKERNSEC_RANDID y
3302+define_bool CONFIG_GRKERNSEC_RANDNET y
3303+define_bool CONFIG_GRKERNSEC_RANDISN y
3304+define_bool CONFIG_GRKERNSEC_RANDSRC y
3305+define_bool CONFIG_GRKERNSEC_RANDRPC y
3306+define_bool CONFIG_GRKERNSEC_RANDPING y
3307+define_bool CONFIG_GRKERNSEC_FORKFAIL y
3308+define_bool CONFIG_GRKERNSEC_TIME y
3309+define_bool CONFIG_GRKERNSEC_SIGNAL y
3310+define_bool CONFIG_GRKERNSEC_CHROOT y
3311+define_bool CONFIG_GRKERNSEC_CHROOT_SHMAT n
3312+define_bool CONFIG_GRKERNSEC_CHROOT_UNIX y
3313+define_bool CONFIG_GRKERNSEC_CHROOT_MOUNT y
3314+define_bool CONFIG_GRKERNSEC_CHROOT_PIVOT y
3315+define_bool CONFIG_GRKERNSEC_CHROOT_DOUBLE y
3316+define_bool CONFIG_GRKERNSEC_CHROOT_CHDIR y
3317+define_bool CONFIG_GRKERNSEC_CHROOT_MKNOD y
3318+define_bool CONFIG_GRKERNSEC_PROC y
3319+define_bool CONFIG_GRKERNSEC_PROC_USERGROUP y
3320+define_int CONFIG_GRKERNSEC_PROC_GID 10
3321+fi
3322+if [ "$CONFIG_GRKERNSEC_HI" = "y" ]; then
3323+define_int CONFIG_GRKERNSEC_FLOODTIME 10
3324+define_int CONFIG_GRKERNSEC_FLOODBURST 4
3325+define_bool CONFIG_GRKERNSEC_LINK y
3326+define_bool CONFIG_GRKERNSEC_FIFO y
3327+define_bool CONFIG_GRKERNSEC_RANDPID y
3328+define_bool CONFIG_GRKERNSEC_EXECVE y
3329+define_bool CONFIG_GRKERNSEC_DMESG y
3330+define_bool CONFIG_GRKERNSEC_RANDID y
3331+define_bool CONFIG_GRKERNSEC_RANDSRC y
3332+define_bool CONFIG_GRKERNSEC_RANDRPC y
3333+define_bool CONFIG_GRKERNSEC_RANDPING y
3334+define_bool CONFIG_GRKERNSEC_FORKFAIL y
3335+define_bool CONFIG_GRKERNSEC_TIME y
3336+define_bool CONFIG_GRKERNSEC_SIGNAL y
3337+define_bool CONFIG_GRKERNSEC_CHROOT_SHMAT y
3338+define_bool CONFIG_GRKERNSEC_CHROOT_UNIX y
3339+define_bool CONFIG_GRKERNSEC_CHROOT_MOUNT y
3340+define_bool CONFIG_GRKERNSEC_CHROOT_FCHDIR y
3341+define_bool CONFIG_GRKERNSEC_CHROOT_PIVOT y
3342+define_bool CONFIG_GRKERNSEC_CHROOT_DOUBLE y
3343+define_bool CONFIG_GRKERNSEC_CHROOT_CHDIR y
3344+define_bool CONFIG_GRKERNSEC_CHROOT_MKNOD y
3345+define_bool CONFIG_GRKERNSEC_CHROOT_CAPS y
3346+define_bool CONFIG_GRKERNSEC_CHROOT_SYSCTL y
3347+define_bool CONFIG_GRKERNSEC_CHROOT_FINDTASK y
3348+define_bool CONFIG_GRKERNSEC_PROC y
3349+define_bool CONFIG_GRKERNSEC_PROC_IPADDR n
3350+define_bool CONFIG_GRKERNSEC_HIDESYM y
3351+define_bool CONFIG_GRKERNSEC_PROC_USERGROUP y
3352+define_int CONFIG_GRKERNSEC_PROC_GID 10
3353+define_bool CONFIG_GRKERNSEC_KMEM y
3354+define_bool CONFIG_GRKERNSEC_RESLOG y
3355+define_bool CONFIG_GRKERNSEC_RANDNET y
3356+define_bool CONFIG_GRKERNSEC_RANDISN y
3357+
3358+define_bool CONFIG_GRKERNSEC_AUDIT_MOUNT n
3359+define_bool CONFIG_GRKERNSEC_ACL_HIDEKERN n
3360+define_int CONFIG_GRKERNSEC_ACL_MAXTRIES 3
3361+define_int CONFIG_GRKERNSEC_ACL_TIMEOUT 30
3362+
3363+define_bool CONFIG_GRKERNSEC_PROC_ADD y
3364+define_bool CONFIG_GRKERNSEC_CHROOT_CHMOD y
3365+define_bool CONFIG_GRKERNSEC_CHROOT_NICE y
3366+if [ "$CONFIG_X86" = "y" ]; then
3367+define_bool CONFIG_GRKERNSEC_IO n
3368+fi
3369+define_bool CONFIG_GRKERNSEC_AUDIT_MOUNT y
3370+fi
3371+if [ "$CONFIG_GRKERNSEC_CUSTOM" = "y" ]; then
3372+mainmenu_option next_comment
3373+comment 'Address Space Protection'
3374+bool 'Deny writing to /dev/kmem, /dev/mem, and /dev/port' CONFIG_GRKERNSEC_KMEM
3375+if [ "$CONFIG_X86" = "y" ]; then
3376+ bool 'Disable privileged I/O' CONFIG_GRKERNSEC_IO
3377+ if [ "$CONFIG_GRKERNSEC_IO" = "y" ]; then
3378+ define_bool CONFIG_RTC y
3379+ fi
3380+fi
3381+bool 'Hide kernel symbols' CONFIG_GRKERNSEC_HIDESYM
3382+endmenu
3383+mainmenu_option next_comment
3384+comment 'Role Based Access Control Options'
3385+bool 'Hide kernel processes' CONFIG_GRKERNSEC_ACL_HIDEKERN
3386+int 'Maximum tries before password lockout' CONFIG_GRKERNSEC_ACL_MAXTRIES 3
3387+int 'Time to wait after max password tries, in seconds' CONFIG_GRKERNSEC_ACL_TIMEOUT 30
3388+endmenu
3389+mainmenu_option next_comment
3390+comment 'Filesystem Protections'
3391+bool 'Proc restrictions' CONFIG_GRKERNSEC_PROC
3392+if [ "$CONFIG_GRKERNSEC_PROC" != "n" ]; then
3393+ bool ' Restrict to user only' CONFIG_GRKERNSEC_PROC_USER
3394+ if [ "$CONFIG_GRKERNSEC_PROC_USER" != "y" ]; then
3395+ bool ' Allow special group' CONFIG_GRKERNSEC_PROC_USERGROUP
3396+ if [ "$CONFIG_GRKERNSEC_PROC_USERGROUP" != "n" ]; then
3397+ int ' GID for special group' CONFIG_GRKERNSEC_PROC_GID 1001
3398+ fi
3399+ fi
3400+ if [ "$CONFIG_GRKERNSEC_PROC_USER" != "n" -o "$CONFIG_GRKERNSEC_PROC_USERGROUP" != "n" ]; then
3401+ bool ' Additional restrictions' CONFIG_GRKERNSEC_PROC_ADD
3402+ fi
3403+fi
3404+bool 'Linking restrictions' CONFIG_GRKERNSEC_LINK
3405+bool 'FIFO restrictions' CONFIG_GRKERNSEC_FIFO
3406+bool 'Chroot jail restrictions' CONFIG_GRKERNSEC_CHROOT
3407+if [ "$CONFIG_GRKERNSEC_CHROOT" != "n" ]; then
3408+bool ' Deny mounts' CONFIG_GRKERNSEC_CHROOT_MOUNT
3409+bool ' Deny double-chroots' CONFIG_GRKERNSEC_CHROOT_DOUBLE
3410+bool ' Deny pivot_root in chroot' CONFIG_GRKERNSEC_CHROOT_PIVOT
3411+bool ' Enforce chdir("/") on all chroots' CONFIG_GRKERNSEC_CHROOT_CHDIR
3412+bool ' Deny (f)chmod +s' CONFIG_GRKERNSEC_CHROOT_CHMOD
3413+bool ' Deny fchdir out of chroot' CONFIG_GRKERNSEC_CHROOT_FCHDIR
3414+bool ' Deny mknod' CONFIG_GRKERNSEC_CHROOT_MKNOD
3415+bool ' Deny shmat() out of chroot' CONFIG_GRKERNSEC_CHROOT_SHMAT
3416+bool ' Deny access to abstract AF_UNIX sockets out of chroot' CONFIG_GRKERNSEC_CHROOT_UNIX
3417+bool ' Protect outside processes' CONFIG_GRKERNSEC_CHROOT_FINDTASK
3418+bool ' Restrict priority changes' CONFIG_GRKERNSEC_CHROOT_NICE
3419+bool ' Deny sysctl writes in chroot' CONFIG_GRKERNSEC_CHROOT_SYSCTL
3420+bool ' Capability restrictions within chroot' CONFIG_GRKERNSEC_CHROOT_CAPS
3421+fi
3422+endmenu
3423+mainmenu_option next_comment
3424+comment 'Kernel Auditing'
3425+bool 'Single group for auditing' CONFIG_GRKERNSEC_AUDIT_GROUP
3426+if [ "$CONFIG_GRKERNSEC_AUDIT_GROUP" != "n" ]; then
3427+int ' GID for auditing' CONFIG_GRKERNSEC_AUDIT_GID 1007
3428+fi
3429+bool 'Exec logging' CONFIG_GRKERNSEC_EXECLOG
3430+bool 'Resource logging' CONFIG_GRKERNSEC_RESLOG
3431+bool 'Log execs within chroot' CONFIG_GRKERNSEC_CHROOT_EXECLOG
3432+bool 'Chdir logging' CONFIG_GRKERNSEC_AUDIT_CHDIR
3433+bool '(Un)Mount logging' CONFIG_GRKERNSEC_AUDIT_MOUNT
3434+bool 'IPC logging' CONFIG_GRKERNSEC_AUDIT_IPC
3435+bool 'Signal logging' CONFIG_GRKERNSEC_SIGNAL
3436+bool 'Fork failure logging' CONFIG_GRKERNSEC_FORKFAIL
3437+bool 'Time change logging' CONFIG_GRKERNSEC_TIME
3438+bool '/proc/<pid>/ipaddr support' CONFIG_GRKERNSEC_PROC_IPADDR
3439+endmenu
3440+mainmenu_option next_comment
3441+comment 'Executable Protections'
3442+bool 'Enforce RLIMIT_NPROC on execs' CONFIG_GRKERNSEC_EXECVE
3443+bool 'Dmesg(8) restriction' CONFIG_GRKERNSEC_DMESG
3444+bool 'Randomized PIDs' CONFIG_GRKERNSEC_RANDPID
3445+bool 'Trusted path execution' CONFIG_GRKERNSEC_TPE
3446+if [ "$CONFIG_GRKERNSEC_TPE" != "n" ]; then
3447+bool ' Partially restrict non-root users' CONFIG_GRKERNSEC_TPE_ALL
3448+int ' GID for untrusted users:' CONFIG_GRKERNSEC_TPE_GID 1005
3449+fi
3450+endmenu
3451+mainmenu_option next_comment
3452+comment 'Network Protections'
3453+bool 'Larger entropy pools' CONFIG_GRKERNSEC_RANDNET
3454+bool 'Truly random TCP ISN selection' CONFIG_GRKERNSEC_RANDISN
3455+bool 'Randomized IP IDs' CONFIG_GRKERNSEC_RANDID
3456+bool 'Randomized TCP source ports' CONFIG_GRKERNSEC_RANDSRC
3457+bool 'Randomized RPC XIDs' CONFIG_GRKERNSEC_RANDRPC
3458+bool 'Altered Ping IDs' CONFIG_GRKERNSEC_RANDPING
3459+bool 'Socket restrictions' CONFIG_GRKERNSEC_SOCKET
3460+if [ "$CONFIG_GRKERNSEC_SOCKET" != "n" ]; then
3461+bool ' Deny any sockets to group' CONFIG_GRKERNSEC_SOCKET_ALL
3462+if [ "$CONFIG_GRKERNSEC_SOCKET_ALL" != "n" ]; then
3463+int ' GID to deny all sockets for:' CONFIG_GRKERNSEC_SOCKET_ALL_GID 1004
3464+fi
3465+bool ' Deny client sockets to group' CONFIG_GRKERNSEC_SOCKET_CLIENT
3466+if [ "$CONFIG_GRKERNSEC_SOCKET_CLIENT" != "n" ]; then
3467+int ' GID to deny client sockets for:' CONFIG_GRKERNSEC_SOCKET_CLIENT_GID 1003
3468+fi
3469+bool ' Deny server sockets to group' CONFIG_GRKERNSEC_SOCKET_SERVER
3470+if [ "$CONFIG_GRKERNSEC_SOCKET_SERVER" != "n" ]; then
3471+int ' GID to deny server sockets for:' CONFIG_GRKERNSEC_SOCKET_SERVER_GID 1002
3472+fi
3473+fi
3474+endmenu
3475+if [ "$CONFIG_SYSCTL" != "n" ]; then
3476+mainmenu_option next_comment
3477+comment 'Sysctl support'
3478+bool 'Sysctl support' CONFIG_GRKERNSEC_SYSCTL
3479+endmenu
3480+fi
3481+mainmenu_option next_comment
3482+comment 'Logging options'
3483+int 'Seconds in between log messages (minimum)' CONFIG_GRKERNSEC_FLOODTIME 10
3484+int 'Number of messages in a burst (maximum)' CONFIG_GRKERNSEC_FLOODBURST 4
3485+endmenu
3486+fi
3487diff -urN linux-2.4.21/grsecurity/Makefile linux-2.4.21/grsecurity/Makefile
3488--- linux-2.4.21/grsecurity/Makefile 1969-12-31 19:00:00.000000000 -0500
3489+++ linux-2.4.21/grsecurity/Makefile 2003-06-23 11:49:16.000000000 -0400
3490@@ -0,0 +1,24 @@
3491+# grsecurity's ACL system was originally written in 2001 by Michael Dalton
3492+# during 2001, 2002, and 2003 it has been completely redesigned by
3493+# Brad Spengler
3494+#
3495+# All code in this directory and various hooks inserted throughout the kernel
3496+# are copyright Brad Spengler, and released under the GPL, unless otherwise
3497+# noted (as in obsd_rand.c)
3498+
3499+O_TARGET := grsec.o
3500+
3501+obj-y = grsec_chdir.o grsec_chroot.o grsec_exec.o grsec_fifo.o grsec_fork.o \
3502+ grsec_mount.o grsec_rand.o grsec_sig.o grsec_sock.o grsec_sysctl.o \
3503+ grsec_time.o grsec_tpe.o grsec_ipc.o grsec_link.o
3504+
3505+ifeq ($(CONFIG_GRKERNSEC),y)
3506+obj-y += grsec_init.o grsum.o gracl.o gracl_ip.o gracl_segv.o obsd_rand.o \
3507+ gracl_cap.o gracl_alloc.o gracl_shm.o grsec_mem.o gracl_fs.o \
3508+ gracl_learn.o
3509+obj-$(CONFIG_GRKERNSEC_RESLOG) += gracl_res.o
3510+else
3511+obj-y += grsec_disabled.o
3512+endif
3513+
3514+include $(TOPDIR)/Rules.make
3515diff -urN linux-2.4.21/grsecurity/gracl.c linux-2.4.21/grsecurity/gracl.c
3516--- linux-2.4.21/grsecurity/gracl.c 1969-12-31 19:00:00.000000000 -0500
3517+++ linux-2.4.21/grsecurity/gracl.c 2003-06-23 11:49:16.000000000 -0400
3518@@ -0,0 +1,2601 @@
3519+/*
3520+ * grsecurity/gracl.c
3521+ * Copyright Brad Spengler 2001, 2002, 2003
3522+ *
3523+ */
3524+
3525+#include <linux/kernel.h>
3526+#include <linux/sched.h>
3527+#include <linux/mm.h>
3528+#include <linux/file.h>
3529+#include <linux/fs.h>
3530+#include <linux/proc_fs.h>
3531+#include <linux/smp_lock.h>
3532+#include <linux/slab.h>
3533+#include <linux/vmalloc.h>
3534+#include <linux/types.h>
3535+#include <linux/capability.h>
3536+#include <linux/sysctl.h>
3537+#include <linux/gracl.h>
3538+#include <linux/gralloc.h>
3539+#include <linux/grsecurity.h>
3540+#include <linux/grinternal.h>
3541+
3542+#include <asm/uaccess.h>
3543+#include <asm/errno.h>
3544+#include <asm/mman.h>
3545+
3546+static struct acl_role_db acl_role_set;
3547+static struct acl_role_label *role_list_head;
3548+static struct name_db name_set;
3549+static struct name_db inodev_set;
3550+
3551+static struct acl_role_label *default_role;
3552+
3553+static u16 acl_sp_role_value;
3554+
3555+static spinlock_t gr_dev_lock = SPIN_LOCK_UNLOCKED;
3556+rwlock_t gr_inode_lock = RW_LOCK_UNLOCKED;
3557+
3558+extern char *gr_shared_page[2][NR_CPUS];
3559+
3560+static unsigned long gr_status = GR_STATUS_INIT;
3561+
3562+extern int chkpw(struct gr_arg *entry, unsigned char *salt, unsigned char *sum);
3563+
3564+#ifdef CONFIG_GRKERNSEC_RESLOG
3565+extern __inline__ void gr_log_resource(const struct task_struct *task,
3566+ const int res,
3567+ const unsigned long wanted);
3568+#endif
3569+
3570+static unsigned char system_salt[GR_SALT_LEN];
3571+static unsigned char system_sum[GR_SHA_LEN];
3572+
3573+static struct sprole_pw **acl_special_roles = NULL;
3574+static __u16 num_sprole_pws = 0;
3575+
3576+static struct acl_role_label *kernel_role = NULL;
3577+
3578+/* The following are used to keep a place held in the hash table when we move
3579+ entries around. They can be replaced during insert. */
3580+
3581+static struct acl_subject_label *deleted_subject;
3582+static struct acl_object_label *deleted_object;
3583+static struct name_entry *deleted_inodev;
3584+
3585+/* for keeping track of the last and final allocated subjects, since
3586+ nested subject parsing is tricky
3587+*/
3588+static struct acl_subject_label *s_last = NULL;
3589+static struct acl_subject_label *s_final = NULL;
3590+
3591+static unsigned int gr_auth_attempts = 0;
3592+static unsigned long gr_auth_expires = 0UL;
3593+
3594+extern int gr_init_uidset(void);
3595+extern void gr_free_uidset(void);
3596+extern void gr_remove_uid(uid_t uid);
3597+extern int gr_find_uid(uid_t uid);
3598+
3599+__inline__ int
3600+gr_acl_is_enabled(void)
3601+{
3602+ return (gr_status & GR_READY);
3603+}
3604+
3605+static __inline__ char *
3606+d_real_path(const struct dentry *dentry, const struct vfsmount *vfsmnt,
3607+ char *buf, int buflen)
3608+{
3609+ char *res;
3610+ struct dentry *our_dentry;
3611+ struct vfsmount *our_mount;
3612+ struct vfsmount *rootmnt;
3613+ struct dentry *root;
3614+
3615+ our_dentry = (struct dentry *) dentry;
3616+ our_mount = (struct vfsmount *) vfsmnt;
3617+
3618+ read_lock(&child_reaper->fs->lock);
3619+ rootmnt = mntget(child_reaper->fs->rootmnt);
3620+ root = dget(child_reaper->fs->root);
3621+ read_unlock(&child_reaper->fs->lock);
3622+
3623+ spin_lock(&dcache_lock);
3624+ res = __d_path(our_dentry, our_mount, root, rootmnt, buf, buflen);
3625+ spin_unlock(&dcache_lock);
3626+ dput(root);
3627+ mntput(rootmnt);
3628+ return res;
3629+}
3630+
3631+char *
3632+gr_to_filename(const struct dentry *dentry, const struct vfsmount *mnt)
3633+{
3634+ return d_real_path(dentry, mnt, gr_shared_page[0][smp_processor_id()],
3635+ PAGE_SIZE);
3636+}
3637+
3638+char *
3639+gr_to_filename1(const struct dentry *dentry, const struct vfsmount *mnt)
3640+{
3641+ return d_real_path(dentry, mnt, gr_shared_page[1][smp_processor_id()],
3642+ PAGE_SIZE);
3643+}
3644+
3645+__inline__ __u32
3646+to_gr_audit(const __u32 reqmode)
3647+{
3648+ __u32 retmode = 0;
3649+
3650+ retmode |= (reqmode & GR_READ) ? GR_AUDIT_READ : 0;
3651+ retmode |= (reqmode & GR_WRITE) ? GR_AUDIT_WRITE | GR_AUDIT_APPEND : 0;
3652+ retmode |= (reqmode & GR_APPEND) ? GR_AUDIT_APPEND : 0;
3653+ retmode |= (reqmode & GR_EXEC) ? GR_AUDIT_EXEC : 0;
3654+ retmode |= (reqmode & GR_INHERIT) ? GR_AUDIT_INHERIT : 0;
3655+ retmode |= (reqmode & GR_FIND) ? GR_AUDIT_FIND : 0;
3656+ retmode |= (reqmode & GR_SETID) ? GR_AUDIT_SETID : 0;
3657+ retmode |= (reqmode & GR_CREATE) ? GR_AUDIT_CREATE : 0;
3658+ retmode |= (reqmode & GR_DELETE) ? GR_AUDIT_DELETE : 0;
3659+
3660+ return retmode;
3661+}
3662+
3663+__inline__ struct acl_role_label *
3664+lookup_acl_role_label(const struct task_struct *task, const uid_t uid,
3665+ const gid_t gid)
3666+{
3667+ unsigned long index = rhash(uid, GR_ROLE_USER, acl_role_set.r_size);
3668+ struct acl_role_label *match;
3669+ struct role_allowed_ip *ipp;
3670+ __u8 i = 0;
3671+
3672+ match = acl_role_set.r_hash[index];
3673+
3674+ while (match
3675+ && (match->uidgid != uid || !(match->roletype & GR_ROLE_USER))) {
3676+ index = (index + (1 << i)) % acl_role_set.r_size;
3677+ match = acl_role_set.r_hash[index];
3678+ i = (i + 1) % 32;
3679+ }
3680+
3681+ if (!match || match->uidgid != uid || !(match->roletype & GR_ROLE_USER)) {
3682+ try_group:
3683+ index = rhash(gid, GR_ROLE_GROUP, acl_role_set.r_size);
3684+ match = acl_role_set.r_hash[index];
3685+ i = 0;
3686+
3687+ while (match
3688+ && (match->uidgid != gid
3689+ || !(match->roletype & GR_ROLE_GROUP))) {
3690+ index = (index + (1 << i)) % acl_role_set.r_size;
3691+ match = acl_role_set.r_hash[index];
3692+ i = (i + 1) % 32;
3693+ }
3694+
3695+ if (!match || match->uidgid != gid
3696+ || !(match->roletype & GR_ROLE_GROUP))
3697+ match = default_role;
3698+ else if (likely(!match->allowed_ips)) {
3699+ return match;
3700+ } else {
3701+ for (ipp = match->allowed_ips; ipp; ipp = ipp->next) {
3702+ if (likely
3703+ ((task->curr_ip & ipp->netmask) ==
3704+ (ipp->addr & ipp->netmask)))
3705+ return match;
3706+ }
3707+ match = default_role;
3708+ }
3709+ } else if (likely(!match->allowed_ips)) {
3710+ return match;
3711+ } else {
3712+ for (ipp = match->allowed_ips; ipp; ipp = ipp->next) {
3713+ if (likely
3714+ ((task->curr_ip & ipp->netmask) ==
3715+ (ipp->addr & ipp->netmask)))
3716+ return match;
3717+ }
3718+ goto try_group;
3719+ }
3720+
3721+ return match;
3722+}
3723+
3724+__inline__ struct acl_subject_label *
3725+lookup_acl_subj_label(const ino_t ino, const kdev_t dev,
3726+ const struct acl_role_label *role)
3727+{
3728+ unsigned long subj_size = role->subj_hash_size;
3729+ struct acl_subject_label **s_hash = role->subj_hash;
3730+ unsigned long index = fhash(ino, dev, subj_size);
3731+ struct acl_subject_label *match;
3732+ __u8 i = 0;
3733+
3734+ match = s_hash[index];
3735+
3736+ while (match && (match->inode != ino || match->device != dev ||
3737+ (match->mode & GR_DELETED))) {
3738+ index = (index + (1 << i)) % subj_size;
3739+ match = s_hash[index];
3740+ i = (i + 1) % 32;
3741+ }
3742+
3743+ if (unlikely(match && (match != deleted_subject) &&
3744+ (match->inode == ino) && (match->device == dev) &&
3745+ !(match->mode & GR_DELETED)))
3746+ return match;
3747+ else
3748+ return NULL;
3749+}
3750+
3751+static __inline__ struct acl_object_label *
3752+lookup_acl_obj_label(const ino_t ino, const kdev_t dev,
3753+ const struct acl_subject_label *subj)
3754+{
3755+ unsigned long obj_size = subj->obj_hash_size;
3756+ struct acl_object_label **o_hash = subj->obj_hash;
3757+ unsigned long index = fhash(ino, dev, obj_size);
3758+ struct acl_object_label *match;
3759+ __u8 i = 0;
3760+
3761+ match = o_hash[index];
3762+
3763+ while (match && (match->inode != ino || match->device != dev ||
3764+ (match->mode & GR_DELETED))) {
3765+ index = (index + (1 << i)) % obj_size;
3766+ match = o_hash[index];
3767+ i = (i + 1) % 32;
3768+ }
3769+
3770+ if (unlikely(match && (match != deleted_object) &&
3771+ (match->inode == ino) && (match->device == dev) &&
3772+ !(match->mode & GR_DELETED)))
3773+ return match;
3774+ else
3775+ return NULL;
3776+}
3777+
3778+static __inline__ struct acl_object_label *
3779+lookup_acl_obj_label_create(const ino_t ino, const kdev_t dev,
3780+ const struct acl_subject_label *subj)
3781+{
3782+ unsigned long obj_size = subj->obj_hash_size;
3783+ struct acl_object_label **o_hash = subj->obj_hash;
3784+ unsigned long index = fhash(ino, dev, obj_size);
3785+ struct acl_object_label *match;
3786+ __u8 i = 0;
3787+
3788+ match = o_hash[index];
3789+
3790+ while (match && (match->inode != ino || match->device != dev ||
3791+ !(match->mode & GR_DELETED))) {
3792+ index = (index + (1 << i)) % obj_size;
3793+ match = o_hash[index];
3794+ i = (i + 1) % 32;
3795+ }
3796+
3797+ if (unlikely(match && (match != deleted_object) &&
3798+ (match->inode == ino) && (match->device == dev) &&
3799+ (match->mode & GR_DELETED)))
3800+ return match;
3801+
3802+ i = 0;
3803+ index = fhash(ino, dev, obj_size);
3804+ match = o_hash[index];
3805+
3806+ while (match && (match->inode != ino || match->device != dev ||
3807+ (match->mode & GR_DELETED))) {
3808+ index = (index + (1 << i)) % obj_size;
3809+ match = o_hash[index];
3810+ i = (i + 1) % 32;
3811+ }
3812+
3813+ if (unlikely(match && (match != deleted_object) &&
3814+ (match->inode == ino) && (match->device == dev) &&
3815+ !(match->mode & GR_DELETED)))
3816+ return match;
3817+ else
3818+ return NULL;
3819+}
3820+
3821+static __inline__ struct name_entry *
3822+lookup_name_entry(const char *name)
3823+{
3824+ unsigned long index = nhash(name, name_set.n_size);
3825+ struct name_entry *match;
3826+ __u8 i = 0;
3827+
3828+ match = name_set.n_hash[index];
3829+
3830+ while (match && (strcmp(match->name, name)) != 0) {
3831+ index = (index + (1 << i)) % name_set.n_size;
3832+ match = name_set.n_hash[index];
3833+ i = (i + 1) % 32;
3834+ }
3835+
3836+ if (unlikely(match && !strcmp(name, match->name)))
3837+ return match;
3838+ else
3839+ return NULL;
3840+}
3841+
3842+static __inline__ struct name_entry *
3843+lookup_inodev_entry(const ino_t ino, const kdev_t dev)
3844+{
3845+ unsigned long index = fhash(ino, dev, inodev_set.n_size);
3846+ struct name_entry *match;
3847+ __u8 i = 0;
3848+
3849+ match = inodev_set.n_hash[index];
3850+
3851+ while (match && (match->inode != ino || match->device != dev)) {
3852+ index = (index + (1 << i)) % inodev_set.n_size;
3853+ match = inodev_set.n_hash[index];
3854+ i = (i + 1) % 32;
3855+ }
3856+
3857+ if (unlikely(match && (match != deleted_inodev) &&
3858+ (match->inode == ino) && (match->device == dev)))
3859+ return match;
3860+ else
3861+ return NULL;
3862+}
3863+
3864+static void
3865+insert_inodev_entry(struct name_entry *nentry)
3866+{
3867+ unsigned long index = fhash(nentry->inode, nentry->device,
3868+ inodev_set.n_size);
3869+ struct name_entry **curr;
3870+ __u8 i = 0;
3871+
3872+ curr = &inodev_set.n_hash[index];
3873+
3874+ while (*curr && *curr != deleted_inodev) {
3875+ index = (index + (1 << i)) % inodev_set.n_size;
3876+ curr = &inodev_set.n_hash[index];
3877+ i = (i + 1) % 32;
3878+ }
3879+
3880+ *curr = nentry;
3881+
3882+ return;
3883+}
3884+
3885+static void
3886+insert_acl_role_label(struct acl_role_label *role)
3887+{
3888+ unsigned long index =
3889+ rhash(role->uidgid, role->roletype & (GR_ROLE_USER | GR_ROLE_GROUP), acl_role_set.r_size);
3890+ struct acl_role_label **curr;
3891+ __u8 i = 0;
3892+
3893+ curr = &acl_role_set.r_hash[index];
3894+
3895+ while (*curr) {
3896+ index = (index + (1 << i)) % acl_role_set.r_size;
3897+ curr = &acl_role_set.r_hash[index];
3898+ i = (i + 1) % 32;
3899+ }
3900+
3901+ *curr = role;
3902+
3903+ return;
3904+}
3905+
3906+static int
3907+insert_name_entry(char *name, const ino_t inode, const kdev_t device)
3908+{
3909+ unsigned long index = nhash(name, name_set.n_size);
3910+ struct name_entry **curr;
3911+ __u8 i = 0;
3912+
3913+ curr = &name_set.n_hash[index];
3914+
3915+ while (*curr && strcmp(name, (*curr)->name)) {
3916+ index = (index + (1 << i)) % name_set.n_size;
3917+ curr = &name_set.n_hash[index];
3918+ i = (i + 1) % 32;
3919+ }
3920+
3921+ if (!(*curr)) {
3922+ struct name_entry *nentry =
3923+ acl_alloc(sizeof (struct name_entry));
3924+ if (!nentry)
3925+ return 0;
3926+ nentry->name = name;
3927+ nentry->inode = inode;
3928+ nentry->device = device;
3929+ *curr = nentry;
3930+ /* insert us into the table searchable by inode/dev */
3931+ insert_inodev_entry(nentry);
3932+ }
3933+
3934+ return 1;
3935+}
3936+
3937+static void
3938+insert_acl_obj_label(struct acl_object_label *obj,
3939+ struct acl_subject_label *subj)
3940+{
3941+ unsigned long index =
3942+ fhash(obj->inode, obj->device, subj->obj_hash_size);
3943+ struct acl_object_label **curr;
3944+ __u8 i = 0;
3945+
3946+ curr = &subj->obj_hash[index];
3947+
3948+ while (*curr && *curr != deleted_object) {
3949+ index = (index + (1 << i)) % subj->obj_hash_size;
3950+ curr = &subj->obj_hash[index];
3951+ i = (i + 1) % 32;
3952+ }
3953+
3954+ *curr = obj;
3955+
3956+ return;
3957+}
3958+
3959+static void
3960+insert_acl_subj_label(struct acl_subject_label *obj,
3961+ struct acl_role_label *role)
3962+{
3963+ unsigned long subj_size = role->subj_hash_size;
3964+ struct acl_subject_label **s_hash = role->subj_hash;
3965+ unsigned long index = fhash(obj->inode, obj->device, subj_size);
3966+ struct acl_subject_label **curr;
3967+ __u8 i = 0;
3968+
3969+ curr = &s_hash[index];
3970+
3971+ while (*curr && *curr != deleted_subject) {
3972+ index = (index + (1 << i)) % subj_size;
3973+ curr = &s_hash[index];
3974+ i = (i + 1) % 32;
3975+ }
3976+
3977+ *curr = obj;
3978+
3979+ return;
3980+}
3981+
3982+static void **
3983+create_table(__u32 * len)
3984+{
3985+ unsigned long table_sizes[] = {
3986+ 7, 13, 31, 61, 127, 251, 509, 1021, 2039, 4093, 8191, 16381,
3987+ 32749, 65521, 131071, 262139, 524287, 1048573, 2097143,
3988+ 4194301, 8388593, 16777213, 33554393, 67108859, 134217689,
3989+ 268435399, 536870909, 1073741789, 2147483647
3990+ };
3991+ void *newtable = NULL;
3992+ unsigned int pwr = 0;
3993+
3994+ while ((pwr < ((sizeof (table_sizes) / sizeof (table_sizes[0])) - 1)) &&
3995+ table_sizes[pwr] <= (2 * (*len)))
3996+ pwr++;
3997+
3998+ if (table_sizes[pwr] <= (2 * (*len)))
3999+ return newtable;
4000+
4001+ if ((table_sizes[pwr] * sizeof (void *)) <= PAGE_SIZE)
4002+ newtable =
4003+ kmalloc(table_sizes[pwr] * sizeof (void *), GFP_KERNEL);
4004+ else
4005+ newtable = vmalloc(table_sizes[pwr] * sizeof (void *));
4006+
4007+ *len = table_sizes[pwr];
4008+
4009+ return newtable;
4010+}
4011+
4012+static int
4013+init_variables(const unsigned long acl_obj_size,
4014+ const unsigned long acl_subj_size,
4015+ const unsigned long acl_ip_size,
4016+ const unsigned long acl_role_size,
4017+ const unsigned long allowed_ip_size,
4018+ const unsigned long acl_trans_size,
4019+ const __u16 num_sprole_pws)
4020+{
4021+ unsigned long stacksize;
4022+
4023+ acl_role_set.r_size = acl_role_size;
4024+ name_set.n_size = (acl_obj_size + acl_subj_size);
4025+ inodev_set.n_size = (acl_obj_size + acl_subj_size);
4026+
4027+ if (!gr_init_uidset())
4028+ return 1;
4029+
4030+ /* set up the stack that holds allocation info */
4031+
4032+ stacksize = (3 * acl_obj_size) + (2 * acl_role_size) +
4033+ (4 * acl_subj_size) + acl_ip_size + (2 * acl_trans_size) +
4034+ allowed_ip_size + (2 * num_sprole_pws) + 5;
4035+
4036+ if (!acl_alloc_stack_init(stacksize))
4037+ return 1;
4038+
4039+ /* create our empty, fake deleted acls */
4040+ deleted_subject =
4041+ (struct acl_subject_label *)
4042+ acl_alloc(sizeof (struct acl_subject_label));
4043+ deleted_object =
4044+ (struct acl_object_label *)
4045+ acl_alloc(sizeof (struct acl_object_label));
4046+ deleted_inodev =
4047+ (struct name_entry *) acl_alloc(sizeof (struct name_entry));
4048+
4049+ if (!deleted_subject || !deleted_object || !deleted_inodev)
4050+ return 1;
4051+
4052+ memset(deleted_subject, 0, sizeof (struct acl_subject_label));
4053+ memset(deleted_object, 0, sizeof (struct acl_object_label));
4054+ memset(deleted_inodev, 0, sizeof (struct name_entry));
4055+
4056+ /* We only want 50% full tables for now */
4057+
4058+ acl_role_set.r_hash =
4059+ (struct acl_role_label **) create_table(&acl_role_set.r_size);
4060+ name_set.n_hash = (struct name_entry **) create_table(&name_set.n_size);
4061+ inodev_set.n_hash =
4062+ (struct name_entry **) create_table(&inodev_set.n_size);
4063+
4064+ if (!acl_role_set.r_hash || !name_set.n_hash || !inodev_set.n_hash)
4065+ return 1;
4066+ memset(acl_role_set.r_hash, 0,
4067+ sizeof (struct acl_role_label *) * acl_role_set.r_size);
4068+ memset(name_set.n_hash, 0,
4069+ sizeof (struct name_entry *) * name_set.n_size);
4070+ memset(inodev_set.n_hash, 0,
4071+ sizeof (struct name_entry *) * inodev_set.n_size);
4072+
4073+ return 0;
4074+}
4075+
4076+static void
4077+free_variables(void)
4078+{
4079+ struct acl_subject_label *s;
4080+ struct acl_role_label *r;
4081+ struct task_struct *task;
4082+
4083+ read_lock(&tasklist_lock);
4084+ for_each_task(task) {
4085+ task->acl_sp_role = 0;
4086+ task->acl_role_id = 0;
4087+ task->acl = NULL;
4088+ task->role = NULL;
4089+ }
4090+ read_unlock(&tasklist_lock);
4091+
4092+ /* free all object hash tables */
4093+
4094+ if (role_list_head) {
4095+ for (r = role_list_head; r; r = r->next) {
4096+ if (!r->subj_hash)
4097+ break;
4098+ for (s = r->proc_subject; s; s = s->next) {
4099+ if (!s->obj_hash)
4100+ break;
4101+ if ((s->obj_hash_size *
4102+ sizeof (struct acl_object_label *)) <=
4103+ PAGE_SIZE)
4104+ kfree(s->obj_hash);
4105+ else
4106+ vfree(s->obj_hash);
4107+ }
4108+ if ((r->subj_hash_size *
4109+ sizeof (struct acl_subject_label *)) <= PAGE_SIZE)
4110+ kfree(r->subj_hash);
4111+ else
4112+ vfree(r->subj_hash);
4113+ }
4114+ }
4115+
4116+ acl_free_all();
4117+
4118+ if (acl_role_set.r_hash) {
4119+ if ((acl_role_set.r_size * sizeof (struct acl_role_label *)) <=
4120+ PAGE_SIZE)
4121+ kfree(acl_role_set.r_hash);
4122+ else
4123+ vfree(acl_role_set.r_hash);
4124+ }
4125+ if (name_set.n_hash) {
4126+ if ((name_set.n_size * sizeof (struct name_entry *)) <=
4127+ PAGE_SIZE)
4128+ kfree(name_set.n_hash);
4129+ else
4130+ vfree(name_set.n_hash);
4131+ }
4132+
4133+ if (inodev_set.n_hash) {
4134+ if ((inodev_set.n_size * sizeof (struct name_entry *)) <=
4135+ PAGE_SIZE)
4136+ kfree(inodev_set.n_hash);
4137+ else
4138+ vfree(inodev_set.n_hash);
4139+ }
4140+
4141+ gr_free_uidset();
4142+
4143+ memset(&name_set, 0, sizeof (struct name_db));
4144+ memset(&inodev_set, 0, sizeof (struct name_db));
4145+ memset(&acl_role_set, 0, sizeof (struct acl_role_db));
4146+
4147+ role_list_head = NULL;
4148+ default_role = NULL;
4149+
4150+ return;
4151+}
4152+
4153+static __u32
4154+count_user_objs(struct acl_object_label *userp)
4155+{
4156+ struct acl_object_label o_tmp;
4157+ __u32 num = 0;
4158+
4159+ while (userp) {
4160+ if (copy_from_user(&o_tmp, userp,
4161+ sizeof (struct acl_object_label)))
4162+ break;
4163+
4164+ userp = o_tmp.prev;
4165+ num++;
4166+ }
4167+
4168+ return num;
4169+}
4170+
4171+static struct acl_subject_label *
4172+do_copy_user_subj(struct acl_subject_label *userp, struct acl_role_label *role);
4173+
4174+static int
4175+copy_user_objs(struct acl_object_label *userp, struct acl_subject_label *subj,
4176+ struct acl_role_label *role)
4177+{
4178+ struct acl_object_label *o_tmp;
4179+ unsigned int len;
4180+ char *tmp;
4181+
4182+ while (userp) {
4183+ if ((o_tmp = (struct acl_object_label *)
4184+ acl_alloc(sizeof (struct acl_object_label))) == NULL)
4185+ return -ENOMEM;
4186+
4187+ if (copy_from_user(o_tmp, userp,
4188+ sizeof (struct acl_object_label)))
4189+ return -EFAULT;
4190+
4191+ userp = o_tmp->prev;
4192+
4193+ len = strnlen_user(o_tmp->filename, PATH_MAX);
4194+
4195+ if (!len || len >= PATH_MAX)
4196+ return -EINVAL;
4197+
4198+ if ((tmp = (char *) acl_alloc(len)) == NULL)
4199+ return -ENOMEM;
4200+
4201+ if (copy_from_user(tmp, o_tmp->filename, len))
4202+ return -EFAULT;
4203+
4204+ o_tmp->filename = tmp;
4205+
4206+ insert_acl_obj_label(o_tmp, subj);
4207+ if (!insert_name_entry(o_tmp->filename, o_tmp->inode,
4208+ o_tmp->device))
4209+ return -ENOMEM;
4210+
4211+ if (o_tmp->nested) {
4212+ o_tmp->nested = do_copy_user_subj(o_tmp->nested, role);
4213+ if (IS_ERR(o_tmp->nested))
4214+ return PTR_ERR(o_tmp->nested);
4215+
4216+ s_final = o_tmp->nested;
4217+ }
4218+ }
4219+
4220+ return 0;
4221+}
4222+
4223+static __u32
4224+count_user_subjs(struct acl_subject_label *userp)
4225+{
4226+ struct acl_subject_label s_tmp;
4227+ __u32 num = 0;
4228+
4229+ while (userp) {
4230+ if (copy_from_user(&s_tmp, userp,
4231+ sizeof (struct acl_subject_label)))
4232+ break;
4233+
4234+ userp = s_tmp.prev;
4235+ /* do not count nested subjects against this count, since
4236+ they are not included in the hash table, but are
4237+ attached to objects. We have already counted
4238+ the subjects in userspace for the allocation
4239+ stack
4240+ */
4241+ if (!s_tmp.parent_subject)
4242+ num++;
4243+ }
4244+
4245+ return num;
4246+}
4247+
4248+static int
4249+copy_user_allowedips(struct acl_role_label *rolep)
4250+{
4251+ struct role_allowed_ip *ruserip, *rtmp = NULL, *rlast;
4252+
4253+ ruserip = rolep->allowed_ips;
4254+
4255+ while (ruserip) {
4256+ rlast = rtmp;
4257+
4258+ if ((rtmp = (struct role_allowed_ip *)
4259+ acl_alloc(sizeof (struct role_allowed_ip))) == NULL)
4260+ return -ENOMEM;
4261+
4262+ if (copy_from_user(rtmp, ruserip,
4263+ sizeof (struct role_allowed_ip)))
4264+ return -EFAULT;
4265+
4266+ ruserip = rtmp->prev;
4267+
4268+ if (!rlast) {
4269+ rtmp->prev = NULL;
4270+ rolep->allowed_ips = rtmp;
4271+ } else {
4272+ rlast->next = rtmp;
4273+ rtmp->prev = rlast;
4274+ }
4275+
4276+ if (!ruserip)
4277+ rtmp->next = NULL;
4278+ }
4279+
4280+ return 0;
4281+}
4282+
4283+static int
4284+copy_user_transitions(struct acl_role_label *rolep)
4285+{
4286+ struct role_transition *rusertp, *rtmp = NULL, *rlast;
4287+ unsigned int len;
4288+ char *tmp;
4289+
4290+ rusertp = rolep->transitions;
4291+
4292+ while (rusertp) {
4293+ rlast = rtmp;
4294+
4295+ if ((rtmp = (struct role_transition *)
4296+ acl_alloc(sizeof (struct role_transition))) == NULL)
4297+ return -ENOMEM;
4298+
4299+ if (copy_from_user(rtmp, rusertp,
4300+ sizeof (struct role_transition)))
4301+ return -EFAULT;
4302+
4303+ rusertp = rtmp->prev;
4304+
4305+ len = strnlen_user(rtmp->rolename, GR_SPROLE_LEN);
4306+
4307+ if (!len || len >= GR_SPROLE_LEN)
4308+ return -EINVAL;
4309+
4310+ if ((tmp = (char *) acl_alloc(len)) == NULL)
4311+ return -ENOMEM;
4312+
4313+ if (copy_from_user(tmp, rtmp->rolename, len))
4314+ return -EFAULT;
4315+
4316+ rtmp->rolename = tmp;
4317+
4318+ if (!rlast) {
4319+ rtmp->prev = NULL;
4320+ rolep->transitions = rtmp;
4321+ } else {
4322+ rlast->next = rtmp;
4323+ rtmp->prev = rlast;
4324+ }
4325+
4326+ if (!rusertp)
4327+ rtmp->next = NULL;
4328+ }
4329+
4330+ return 0;
4331+}
4332+
4333+static struct acl_subject_label *
4334+do_copy_user_subj(struct acl_subject_label *userp, struct acl_role_label *role)
4335+{
4336+ struct acl_subject_label *s_tmp = NULL;
4337+ unsigned int len;
4338+ char *tmp;
4339+ __u32 num_objs;
4340+ struct acl_ip_label **i_tmp, *i_utmp2;
4341+ unsigned long i_num;
4342+ int err;
4343+
4344+
4345+ if ((s_tmp = (struct acl_subject_label *)
4346+ acl_alloc(sizeof (struct acl_subject_label))) == NULL)
4347+ return ERR_PTR(-ENOMEM);
4348+
4349+ if (copy_from_user(s_tmp, userp,
4350+ sizeof (struct acl_subject_label)))
4351+ return ERR_PTR(-EFAULT);
4352+
4353+ if (!s_last) {
4354+ s_tmp->prev = NULL;
4355+ role->proc_subject = s_tmp;
4356+ } else {
4357+ s_last->next = s_tmp;
4358+ s_tmp->prev = s_last;
4359+ }
4360+
4361+ s_last = s_tmp;
4362+
4363+ len = strnlen_user(s_tmp->filename, PATH_MAX);
4364+
4365+ if (!len || len >= PATH_MAX)
4366+ return ERR_PTR(-EINVAL);
4367+
4368+ if ((tmp = (char *) acl_alloc(len)) == NULL)
4369+ return ERR_PTR(-ENOMEM);
4370+
4371+ if (copy_from_user(tmp, s_tmp->filename, len))
4372+ return ERR_PTR(-EFAULT);
4373+
4374+ s_tmp->filename = tmp;
4375+
4376+ if (!strcmp(s_tmp->filename, "/"))
4377+ role->root_label = s_tmp;
4378+
4379+ /* set up object hash table */
4380+ num_objs = count_user_objs(s_tmp->proc_object);
4381+
4382+ s_tmp->obj_hash_size = num_objs;
4383+ s_tmp->obj_hash =
4384+ (struct acl_object_label **)
4385+ create_table(&(s_tmp->obj_hash_size));
4386+
4387+ if (!s_tmp->obj_hash)
4388+ return ERR_PTR(-ENOMEM);
4389+
4390+ memset(s_tmp->obj_hash, 0,
4391+ s_tmp->obj_hash_size *
4392+ sizeof (struct acl_object_label *));
4393+
4394+ /* copy before adding in objects, since a nested
4395+ acl could be found and be the final subject
4396+ copied
4397+ */
4398+
4399+ s_final = s_tmp;
4400+
4401+ /* add in objects */
4402+ err = copy_user_objs(s_tmp->proc_object, s_tmp, role);
4403+
4404+ if (err)
4405+ return ERR_PTR(err);
4406+
4407+ /* add in ip acls */
4408+
4409+ if (!s_tmp->ip_num) {
4410+ s_tmp->ips = NULL;
4411+ goto insert;
4412+ }
4413+
4414+ i_tmp =
4415+ (struct acl_ip_label **) acl_alloc(s_tmp->ip_num *
4416+ sizeof (struct
4417+ acl_ip_label *));
4418+
4419+ if (!i_tmp)
4420+ return ERR_PTR(-ENOMEM);
4421+
4422+ for (i_num = 0; i_num < s_tmp->ip_num; i_num++) {
4423+ *(i_tmp + i_num) =
4424+ (struct acl_ip_label *)
4425+ acl_alloc(sizeof (struct acl_ip_label));
4426+ if (!*(i_tmp + i_num))
4427+ return ERR_PTR(-ENOMEM);
4428+
4429+ if (copy_from_user
4430+ (&i_utmp2, s_tmp->ips + i_num,
4431+ sizeof (struct acl_ip_label *)))
4432+ return ERR_PTR(-EFAULT);
4433+
4434+ if (copy_from_user
4435+ (*(i_tmp + i_num), i_utmp2,
4436+ sizeof (struct acl_ip_label)))
4437+ return ERR_PTR(-EFAULT);
4438+ }
4439+
4440+ s_tmp->ips = i_tmp;
4441+
4442+insert:
4443+ if (!insert_name_entry(s_tmp->filename, s_tmp->inode,
4444+ s_tmp->device))
4445+ return ERR_PTR(-ENOMEM);
4446+
4447+ return s_tmp;
4448+}
4449+
4450+static int
4451+copy_user_subjs(struct acl_subject_label *userp, struct acl_role_label *role)
4452+{
4453+ struct acl_subject_label s_pre;
4454+ struct acl_subject_label * ret;
4455+ int err;
4456+
4457+ while (userp) {
4458+ if (copy_from_user(&s_pre, userp,
4459+ sizeof (struct acl_subject_label)))
4460+ return -EFAULT;
4461+
4462+ /* do not add nested subjects here, add
4463+ while parsing objects
4464+ */
4465+
4466+ if (s_pre.parent_subject) {
4467+ userp = s_pre.prev;
4468+ continue;
4469+ }
4470+
4471+ ret = do_copy_user_subj(userp, role);
4472+
4473+ err = PTR_ERR(ret);
4474+ if (IS_ERR(ret))
4475+ return err;
4476+
4477+ insert_acl_subj_label(ret, role);
4478+
4479+ userp = s_pre.prev;
4480+ }
4481+
4482+ s_final->next = NULL;
4483+
4484+ return 0;
4485+}
4486+
4487+static int
4488+copy_user_acl(struct gr_arg *arg)
4489+{
4490+ struct acl_role_label *r_tmp = NULL, **r_utmp, *r_utmp2, *r_last;
4491+ struct sprole_pw *sptmp;
4492+ unsigned long r_num;
4493+ unsigned int len;
4494+ char *tmp;
4495+ int err = 0;
4496+ __u16 i;
4497+ __u32 num_subjs;
4498+
4499+ /* we need a default and kernel role */
4500+ if (arg->role_db.r_entries < 2)
4501+ return -EINVAL;
4502+
4503+ /* copy special role authentication info from userspace */
4504+
4505+ num_sprole_pws = arg->num_sprole_pws;
4506+ acl_special_roles = (struct sprole_pw **) acl_alloc(num_sprole_pws * sizeof(struct sprole_pw *));
4507+
4508+ if (!acl_special_roles) {
4509+ err = -ENOMEM;
4510+ goto cleanup;
4511+ }
4512+
4513+ for (i = 0; i < num_sprole_pws; i++) {
4514+ sptmp = (struct sprole_pw *) acl_alloc(sizeof(struct sprole_pw));
4515+ if (!sptmp) {
4516+ err = -ENOMEM;
4517+ goto cleanup;
4518+ }
4519+ if (copy_from_user(sptmp, arg->sprole_pws + i,
4520+ sizeof (struct sprole_pw))) {
4521+ err = -EFAULT;
4522+ goto cleanup;
4523+ }
4524+
4525+ len =
4526+ strnlen_user(sptmp->rolename, GR_SPROLE_LEN);
4527+
4528+ if (!len || len >= GR_SPROLE_LEN) {
4529+ err = -EINVAL;
4530+ goto cleanup;
4531+ }
4532+
4533+ if ((tmp = (char *) acl_alloc(len)) == NULL) {
4534+ err = -ENOMEM;
4535+ goto cleanup;
4536+ }
4537+
4538+ if (copy_from_user(tmp, sptmp->rolename, len)) {
4539+ err = -EFAULT;
4540+ goto cleanup;
4541+ }
4542+
4543+#ifdef CONFIG_GRKERNSEC_ACL_DEBUG
4544+ printk(KERN_ALERT "Copying special role %s\n", tmp);
4545+#endif
4546+ sptmp->rolename = tmp;
4547+ acl_special_roles[i] = sptmp;
4548+ }
4549+
4550+ r_utmp = (struct acl_role_label **) arg->role_db.r_table;
4551+
4552+ for (r_num = 0; r_num < arg->role_db.r_entries; r_num++) {
4553+ r_last = r_tmp;
4554+
4555+ r_tmp = acl_alloc(sizeof (struct acl_role_label));
4556+
4557+ if (!r_tmp) {
4558+ err = -ENOMEM;
4559+ goto cleanup;
4560+ }
4561+
4562+ if (copy_from_user(&r_utmp2, r_utmp + r_num,
4563+ sizeof (struct acl_role_label *))) {
4564+ err = -EFAULT;
4565+ goto cleanup;
4566+ }
4567+
4568+ if (copy_from_user(r_tmp, r_utmp2,
4569+ sizeof (struct acl_role_label))) {
4570+ err = -EFAULT;
4571+ goto cleanup;
4572+ }
4573+
4574+ if (!r_last) {
4575+ r_tmp->prev = NULL;
4576+ role_list_head = r_tmp;
4577+ } else {
4578+ r_last->next = r_tmp;
4579+ r_tmp->prev = r_last;
4580+ }
4581+
4582+ if (r_num == (arg->role_db.r_entries - 1))
4583+ r_tmp->next = NULL;
4584+
4585+ len = strnlen_user(r_tmp->rolename, PATH_MAX);
4586+
4587+ if (!len || len >= PATH_MAX) {
4588+ err = -EINVAL;
4589+ goto cleanup;
4590+ }
4591+
4592+ if ((tmp = (char *) acl_alloc(len)) == NULL) {
4593+ err = -ENOMEM;
4594+ goto cleanup;
4595+ }
4596+ if (copy_from_user(tmp, r_tmp->rolename, len)) {
4597+ err = -EFAULT;
4598+ goto cleanup;
4599+ }
4600+ r_tmp->rolename = tmp;
4601+
4602+ if (!strcmp(r_tmp->rolename, "default")
4603+ && (r_tmp->roletype & GR_ROLE_DEFAULT)) {
4604+ default_role = r_tmp;
4605+ } else if (!strcmp(r_tmp->rolename, ":::kernel:::")) {
4606+ kernel_role = r_tmp;
4607+ }
4608+
4609+ num_subjs = count_user_subjs(r_tmp->proc_subject);
4610+
4611+ r_tmp->subj_hash_size = num_subjs;
4612+ r_tmp->subj_hash =
4613+ (struct acl_subject_label **)
4614+ create_table(&(r_tmp->subj_hash_size));
4615+
4616+ if (!r_tmp->subj_hash) {
4617+ err = -ENOMEM;
4618+ goto cleanup;
4619+ }
4620+
4621+ err = copy_user_allowedips(r_tmp);
4622+ if (err)
4623+ goto cleanup;
4624+
4625+ err = copy_user_transitions(r_tmp);
4626+ if (err)
4627+ goto cleanup;
4628+
4629+ memset(r_tmp->subj_hash, 0,
4630+ r_tmp->subj_hash_size *
4631+ sizeof (struct acl_subject_label *));
4632+
4633+ s_last = NULL;
4634+
4635+ err = copy_user_subjs(r_tmp->proc_subject, r_tmp);
4636+
4637+ if (err)
4638+ goto cleanup;
4639+
4640+ insert_acl_role_label(r_tmp);
4641+ }
4642+
4643+ goto return_err;
4644+ cleanup:
4645+ free_variables();
4646+ return_err:
4647+ return err;
4648+
4649+}
4650+
4651+static int
4652+gracl_init(struct gr_arg *args)
4653+{
4654+ int error = 0;
4655+
4656+ memcpy(&system_salt, args->salt, sizeof (system_salt));
4657+ memcpy(&system_sum, args->sum, sizeof (system_sum));
4658+
4659+ if (init_variables(args->role_db.o_entries, args->role_db.s_entries,
4660+ args->role_db.i_entries, args->role_db.r_entries,
4661+ args->role_db.a_entries, args->role_db.t_entries,
4662+ args->num_sprole_pws)) {
4663+ security_alert_good(GR_INITF_ACL_MSG, GR_VERSION);
4664+ error = -ENOMEM;
4665+ free_variables();
4666+ }
4667+
4668+ error = copy_user_acl(args);
4669+ if (error)
4670+ goto out;
4671+
4672+ if ((error = gr_set_acls(0))) {
4673+ free_variables();
4674+ goto out;
4675+ }
4676+
4677+ gr_status |= GR_READY;
4678+ out:
4679+ return error;
4680+}
4681+
4682+static struct acl_object_label *
4683+chk_obj_label(const struct dentry *l_dentry, const struct vfsmount *l_mnt,
4684+ const struct acl_subject_label *subj)
4685+{
4686+ struct dentry *dentry = (struct dentry *) l_dentry;
4687+ struct vfsmount *mnt = (struct vfsmount *) l_mnt;
4688+ struct dentry *root;
4689+ struct vfsmount *rootmnt;
4690+ struct acl_object_label *retval;
4691+
4692+ read_lock(&child_reaper->fs->lock);
4693+ rootmnt = mntget(child_reaper->fs->rootmnt);
4694+ root = dget(child_reaper->fs->root);
4695+ read_unlock(&child_reaper->fs->lock);
4696+ spin_lock(&dcache_lock);
4697+
4698+ for (;;) {
4699+ if (unlikely(dentry == root && mnt == rootmnt))
4700+ break;
4701+ if (unlikely(dentry == mnt->mnt_root || IS_ROOT(dentry))) {
4702+ if (mnt->mnt_parent == mnt)
4703+ break;
4704+
4705+ read_lock(&gr_inode_lock);
4706+ retval =
4707+ lookup_acl_obj_label(dentry->d_inode->i_ino,
4708+ dentry->d_inode->i_dev, subj);
4709+ read_unlock(&gr_inode_lock);
4710+ if (unlikely(retval != NULL))
4711+ goto out;
4712+
4713+ dentry = mnt->mnt_mountpoint;
4714+ mnt = mnt->mnt_parent;
4715+ continue;
4716+ }
4717+
4718+ read_lock(&gr_inode_lock);
4719+ retval =
4720+ lookup_acl_obj_label(dentry->d_inode->i_ino,
4721+ dentry->d_inode->i_dev, subj);
4722+ read_unlock(&gr_inode_lock);
4723+ if (unlikely(retval != NULL))
4724+ goto out;
4725+
4726+ dentry = dentry->d_parent;
4727+ }
4728+
4729+ read_lock(&gr_inode_lock);
4730+ retval =
4731+ lookup_acl_obj_label(dentry->d_inode->i_ino, dentry->d_inode->i_dev,
4732+ subj);
4733+ read_unlock(&gr_inode_lock);
4734+
4735+ if (unlikely(retval == NULL)) {
4736+ read_lock(&gr_inode_lock);
4737+ retval =
4738+ lookup_acl_obj_label(root->d_inode->i_ino,
4739+ root->d_inode->i_dev, subj);
4740+ read_unlock(&gr_inode_lock);
4741+ }
4742+ out:
4743+ spin_unlock(&dcache_lock);
4744+ dput(root);
4745+ mntput(rootmnt);
4746+
4747+ return retval;
4748+}
4749+
4750+static struct acl_subject_label *
4751+chk_subj_label(const struct dentry *l_dentry, const struct vfsmount *l_mnt,
4752+ const struct acl_role_label *role)
4753+{
4754+ struct dentry *dentry = (struct dentry *) l_dentry;
4755+ struct vfsmount *mnt = (struct vfsmount *) l_mnt;
4756+ struct dentry *root;
4757+ struct vfsmount *rootmnt;
4758+ struct acl_subject_label *retval;
4759+
4760+ read_lock(&child_reaper->fs->lock);
4761+ rootmnt = mntget(child_reaper->fs->rootmnt);
4762+ root = dget(child_reaper->fs->root);
4763+ read_unlock(&child_reaper->fs->lock);
4764+ spin_lock(&dcache_lock);
4765+
4766+ for (;;) {
4767+ if (unlikely(dentry == root && mnt == rootmnt))
4768+ break;
4769+ if (unlikely(dentry == mnt->mnt_root || IS_ROOT(dentry))) {
4770+ if (mnt->mnt_parent == mnt)
4771+ break;
4772+
4773+ read_lock(&gr_inode_lock);
4774+ retval =
4775+ lookup_acl_subj_label(dentry->d_inode->i_ino,
4776+ dentry->d_inode->i_dev, role);
4777+ read_unlock(&gr_inode_lock);
4778+ if (unlikely(retval != NULL))
4779+ goto out;
4780+
4781+ dentry = mnt->mnt_mountpoint;
4782+ mnt = mnt->mnt_parent;
4783+ continue;
4784+ }
4785+
4786+ read_lock(&gr_inode_lock);
4787+ retval =
4788+ lookup_acl_subj_label(dentry->d_inode->i_ino,
4789+ dentry->d_inode->i_dev, role);
4790+ read_unlock(&gr_inode_lock);
4791+ if (unlikely(retval != NULL))
4792+ goto out;
4793+
4794+ dentry = dentry->d_parent;
4795+ }
4796+
4797+ read_lock(&gr_inode_lock);
4798+ retval =
4799+ lookup_acl_subj_label(dentry->d_inode->i_ino,
4800+ dentry->d_inode->i_dev, role);
4801+ read_unlock(&gr_inode_lock);
4802+
4803+ if (unlikely(retval == NULL)) {
4804+ read_lock(&gr_inode_lock);
4805+ retval =
4806+ lookup_acl_subj_label(root->d_inode->i_ino,
4807+ root->d_inode->i_dev, role);
4808+ read_unlock(&gr_inode_lock);
4809+ }
4810+ out:
4811+ spin_unlock(&dcache_lock);
4812+ dput(root);
4813+ mntput(rootmnt);
4814+
4815+ return retval;
4816+}
4817+
4818+static __inline__ void
4819+gr_log_learn(const struct acl_role_label *role, const uid_t uid, const gid_t gid,
4820+ const struct task_struct *task, const char *pathname,
4821+ const __u32 mode)
4822+{
4823+ security_learn(GR_LEARN_AUDIT_MSG, role->rolename, role->roletype,
4824+ uid, gid, task->exec_file ? gr_to_filename1(task->exec_file->f_dentry,
4825+ task->exec_file->f_vfsmnt) : task->acl->filename, task->acl->filename,
4826+ 1, 1, pathname, (unsigned long) mode, NIPQUAD(task->curr_ip));
4827+
4828+ return;
4829+}
4830+
4831+__u32
4832+gr_check_link(const struct dentry * new_dentry,
4833+ const struct dentry * parent_dentry,
4834+ const struct vfsmount * parent_mnt,
4835+ const struct dentry * old_dentry, const struct vfsmount * old_mnt)
4836+{
4837+ struct acl_object_label *obj;
4838+ __u32 oldmode, newmode;
4839+
4840+ if (unlikely(!(gr_status & GR_READY)))
4841+ return (GR_WRITE | GR_CREATE);
4842+
4843+ obj = chk_obj_label(old_dentry, old_mnt, current->acl);
4844+ oldmode = obj->mode;
4845+
4846+ if (current->acl->mode & GR_LEARN)
4847+ oldmode |= (GR_WRITE | GR_CREATE);
4848+ newmode =
4849+ gr_check_create(new_dentry, parent_dentry, parent_mnt,
4850+ oldmode | GR_CREATE | GR_AUDIT_CREATE |
4851+ GR_AUDIT_WRITE | GR_SUPPRESS);
4852+
4853+ if ((newmode & oldmode) == oldmode)
4854+ return newmode;
4855+ else if (current->acl->mode & GR_LEARN) {
4856+ gr_log_learn(current->role, current->uid, current->gid,
4857+ current, gr_to_filename(old_dentry, old_mnt), oldmode);
4858+ return (GR_WRITE | GR_CREATE);
4859+ } else if (newmode & GR_SUPPRESS)
4860+ return GR_SUPPRESS;
4861+ else
4862+ return 0;
4863+}
4864+
4865+__u32
4866+gr_search_file(const struct dentry * dentry, const __u32 mode,
4867+ const struct vfsmount * mnt)
4868+{
4869+ __u32 retval = mode;
4870+ struct acl_subject_label *curracl;
4871+ struct acl_object_label *currobj;
4872+
4873+ if (unlikely(!(gr_status & GR_READY)))
4874+ return (mode & ~GR_AUDITS);
4875+
4876+ curracl = current->acl;
4877+
4878+ currobj = chk_obj_label(dentry, mnt, curracl);
4879+ retval = currobj->mode & mode;
4880+
4881+ if (unlikely
4882+ ((curracl->mode & GR_LEARN) && (mode != GR_PTRACERD)
4883+ && (retval != (mode & ~(GR_AUDITS | GR_SUPPRESS))))) {
4884+ __u32 new_mode = mode;
4885+
4886+ new_mode &= ~(GR_AUDITS | GR_SUPPRESS);
4887+
4888+ retval = new_mode;
4889+
4890+ if (!(mode & GR_NOLEARN))
4891+ gr_log_learn(current->role, current->uid, current->gid,
4892+ current, gr_to_filename(dentry, mnt), new_mode);
4893+ }
4894+
4895+ return retval;
4896+}
4897+
4898+__u32
4899+gr_check_create(const struct dentry * new_dentry, const struct dentry * parent,
4900+ const struct vfsmount * mnt, const __u32 mode)
4901+{
4902+ struct name_entry *match;
4903+ struct acl_object_label *matchpo;
4904+ struct acl_subject_label *curracl;
4905+ __u32 retval;
4906+
4907+ if (unlikely(!(gr_status & GR_READY)))
4908+ return (mode & ~GR_AUDITS);
4909+
4910+ match = lookup_name_entry(gr_to_filename(new_dentry, mnt));
4911+
4912+ if (!match)
4913+ goto check_parent;
4914+
4915+ curracl = current->acl;
4916+
4917+ read_lock(&gr_inode_lock);
4918+ matchpo = lookup_acl_obj_label_create(match->inode, match->device, curracl);
4919+ read_unlock(&gr_inode_lock);
4920+
4921+ if (matchpo) {
4922+ if ((matchpo->mode & mode) !=
4923+ (mode & ~(GR_AUDITS | GR_SUPPRESS))
4924+ && curracl->mode & GR_LEARN) {
4925+ __u32 new_mode = mode;
4926+
4927+ new_mode &= ~(GR_AUDITS | GR_SUPPRESS);
4928+
4929+ gr_log_learn(current->role, current->uid, current->gid,
4930+ current, gr_to_filename(new_dentry, mnt), new_mode);
4931+
4932+ return new_mode;
4933+ }
4934+ return (matchpo->mode & mode);
4935+ }
4936+
4937+ check_parent:
4938+ curracl = current->acl;
4939+
4940+ matchpo = chk_obj_label(parent, mnt, curracl);
4941+ retval = matchpo->mode & mode;
4942+
4943+ if ((retval != (mode & ~(GR_AUDITS | GR_SUPPRESS)))
4944+ && (curracl->mode & GR_LEARN)) {
4945+ __u32 new_mode = mode;
4946+
4947+ new_mode &= ~(GR_AUDITS | GR_SUPPRESS);
4948+
4949+ gr_log_learn(current->role, current->uid, current->gid,
4950+ current, gr_to_filename(new_dentry, mnt), new_mode);
4951+ return new_mode;
4952+ }
4953+
4954+ return retval;
4955+}
4956+
4957+int
4958+gr_check_hidden_task(const struct task_struct *task)
4959+{
4960+ if (unlikely(!(gr_status & GR_READY)))
4961+ return 0;
4962+
4963+ if (!(task->acl->mode & GR_FIND) && !(current->acl->mode & GR_VIEW))
4964+ return 1;
4965+
4966+ return 0;
4967+}
4968+
4969+int
4970+gr_check_protected_task(const struct task_struct *task)
4971+{
4972+ if (unlikely(!(gr_status & GR_READY) || !task))
4973+ return 0;
4974+
4975+ if ((task->acl->mode & GR_PROTECTED) && !(current->acl->mode & GR_KILL))
4976+ return 1;
4977+
4978+ return 0;
4979+}
4980+
4981+__inline__ void
4982+gr_copy_label(struct task_struct *tsk)
4983+{
4984+ tsk->used_accept = 0;
4985+ tsk->used_connect = 0;
4986+ tsk->acl_sp_role = 0;
4987+ tsk->acl_role_id = current->acl_role_id;
4988+ tsk->acl = current->acl;
4989+ tsk->role = current->role;
4990+ tsk->curr_ip = current->curr_ip;
4991+ if (current->exec_file)
4992+ get_file(current->exec_file);
4993+ tsk->exec_file = current->exec_file;
4994+ tsk->is_writable = current->is_writable;
4995+ if (unlikely(current->used_accept))
4996+ current->curr_ip = 0;
4997+
4998+ return;
4999+}
5000+
5001+static __inline__ void
5002+gr_set_proc_res(void)
5003+{
5004+ struct acl_subject_label *proc;
5005+ unsigned short i;
5006+
5007+ proc = current->acl;
5008+
5009+ if (proc->mode & GR_LEARN)
5010+ return;
5011+
5012+ for (i = 0; i < RLIM_NLIMITS; i++) {
5013+ if (!(proc->resmask & (1 << i)))
5014+ continue;
5015+
5016+ current->rlim[i].rlim_cur = proc->res[i].rlim_cur;
5017+ current->rlim[i].rlim_max = proc->res[i].rlim_max;
5018+ }
5019+
5020+ return;
5021+}
5022+
5023+static __inline__ void
5024+do_set_role_label(struct task_struct *task, const uid_t uid, const gid_t gid)
5025+{
5026+ task->role = lookup_acl_role_label(task, uid, gid);
5027+
5028+ return;
5029+}
5030+
5031+void
5032+gr_set_role_label(struct task_struct *task, const uid_t uid, const uid_t gid)
5033+{
5034+ struct acl_object_label *obj;
5035+ struct file *filp;
5036+
5037+ if (unlikely(!(gr_status & GR_READY)))
5038+ return;
5039+
5040+ filp = task->exec_file;
5041+
5042+ /* kernel process, we'll give them the kernel role */
5043+ if (unlikely(!filp)) {
5044+ task->role = kernel_role;
5045+ task->acl = kernel_role->root_label;
5046+ return;
5047+ } else if (!task->role || !(task->role->roletype & GR_ROLE_SPECIAL))
5048+ do_set_role_label(task, uid, gid);
5049+
5050+ task->acl =
5051+ chk_subj_label(filp->f_dentry, filp->f_vfsmnt, task->role);
5052+
5053+ task->is_writable = 0;
5054+
5055+ /* ignore additional mmap checks for processes that are writable
5056+ by the default ACL */
5057+ obj = chk_obj_label(filp->f_dentry, filp->f_vfsmnt, default_role->root_label);
5058+ if (unlikely(obj->mode & GR_WRITE))
5059+ task->is_writable = 1;
5060+ obj = chk_obj_label(filp->f_dentry, filp->f_vfsmnt, task->role->root_label);
5061+ if (unlikely(obj->mode & GR_WRITE))
5062+ task->is_writable = 1;
5063+
5064+#ifdef CONFIG_GRKERNSEC_ACL_DEBUG
5065+ printk(KERN_ALERT "Set role label for (%s:%d): role:%s, subject:%s\n", task->comm, task->pid, task->role->rolename, task->acl->filename);
5066+#endif
5067+
5068+ gr_set_proc_res();
5069+
5070+ return;
5071+}
5072+
5073+void
5074+gr_set_proc_label(const struct dentry *dentry, const struct vfsmount *mnt)
5075+{
5076+ struct acl_subject_label *newacl;
5077+ struct acl_object_label *obj;
5078+ __u32 retmode;
5079+
5080+ if (unlikely(!(gr_status & GR_READY)))
5081+ return;
5082+
5083+ newacl = chk_subj_label(dentry, mnt, current->role);
5084+
5085+ obj = chk_obj_label(dentry, mnt, current->acl);
5086+ retmode = obj->mode & (GR_INHERIT | GR_AUDIT_INHERIT);
5087+
5088+ if ((newacl->mode & GR_LEARN) || !(retmode & GR_INHERIT)) {
5089+ if (obj->nested)
5090+ current->acl = obj->nested;
5091+ else
5092+ current->acl = newacl;
5093+ } else if (retmode & GR_INHERIT && retmode & GR_AUDIT_INHERIT)
5094+ security_audit(GR_INHERIT_ACL_MSG, current->acl->filename,
5095+ gr_to_filename(dentry, mnt), DEFAULTSECARGS);
5096+
5097+ current->is_writable = 0;
5098+
5099+ /* ignore additional mmap checks for processes that are writable
5100+ by the default ACL */
5101+ obj = chk_obj_label(dentry, mnt, default_role->root_label);
5102+ if (unlikely(obj->mode & GR_WRITE))
5103+ current->is_writable = 1;
5104+ obj = chk_obj_label(dentry, mnt, current->role->root_label);
5105+ if (unlikely(obj->mode & GR_WRITE))
5106+ current->is_writable = 1;
5107+
5108+ gr_set_proc_res();
5109+
5110+#ifdef CONFIG_GRKERNSEC_ACL_DEBUG
5111+ printk(KERN_ALERT "Set subject label for (%s:%d): role:%s, subject:%s\n", current->comm, current->pid, current->role->rolename, current->acl->filename);
5112+#endif
5113+ return;
5114+}
5115+
5116+static __inline__ void
5117+do_handle_delete(const ino_t ino, const kdev_t dev)
5118+{
5119+ struct acl_object_label *matchpo;
5120+ struct acl_subject_label *matchps;
5121+ struct acl_subject_label *i;
5122+ struct acl_role_label *role;
5123+
5124+ for (role = role_list_head; role; role = role->next) {
5125+ for (i = role->proc_subject; i; i = i->next) {
5126+ if (unlikely(i->parent_subject &&
5127+ (i->inode == ino) &&
5128+ (i->device == dev)))
5129+ i->mode |= GR_DELETED;
5130+ if (unlikely((matchpo =
5131+ lookup_acl_obj_label(ino, dev, i)) != NULL))
5132+ matchpo->mode |= GR_DELETED;
5133+ }
5134+
5135+ if (unlikely((matchps = lookup_acl_subj_label(ino, dev, role)) != NULL))
5136+ matchps->mode |= GR_DELETED;
5137+ }
5138+
5139+ return;
5140+}
5141+
5142+void
5143+gr_handle_delete(const ino_t ino, const kdev_t dev)
5144+{
5145+ if (unlikely(!(gr_status & GR_READY)))
5146+ return;
5147+
5148+ write_lock(&gr_inode_lock);
5149+ if (unlikely((unsigned long)lookup_inodev_entry(ino, dev)))
5150+ do_handle_delete(ino, dev);
5151+ write_unlock(&gr_inode_lock);
5152+
5153+ return;
5154+}
5155+
5156+static __inline__ void
5157+update_acl_obj_label(const ino_t oldinode, const kdev_t olddevice,
5158+ const ino_t newinode, const kdev_t newdevice,
5159+ struct acl_subject_label *subj)
5160+{
5161+ unsigned long index = fhash(oldinode, olddevice, subj->obj_hash_size);
5162+ struct acl_object_label **match;
5163+ struct acl_object_label *tmp;
5164+ __u8 i = 0;
5165+
5166+ match = &subj->obj_hash[index];
5167+
5168+ while (*match && ((*match)->inode != oldinode ||
5169+ (*match)->device != olddevice ||
5170+ !((*match)->mode & GR_DELETED))) {
5171+ index = (index + (1 << i)) % subj->obj_hash_size;
5172+ match = &subj->obj_hash[index];
5173+ i = (i + 1) % 32;
5174+ }
5175+
5176+ if (*match && ((*match) != deleted_object)
5177+ && ((*match)->inode == oldinode)
5178+ && ((*match)->device == olddevice)
5179+ && ((*match)->mode & GR_DELETED)) {
5180+ tmp = *match;
5181+ tmp->inode = newinode;
5182+ tmp->device = newdevice;
5183+ tmp->mode &= ~GR_DELETED;
5184+
5185+ *match = deleted_object;
5186+
5187+ insert_acl_obj_label(tmp, subj);
5188+ }
5189+
5190+ return;
5191+}
5192+
5193+static __inline__ void
5194+update_acl_subj_label(const ino_t oldinode, const kdev_t olddevice,
5195+ const ino_t newinode, const kdev_t newdevice,
5196+ struct acl_role_label *role)
5197+{
5198+ struct acl_subject_label **s_hash = role->subj_hash;
5199+ unsigned long subj_size = role->subj_hash_size;
5200+ unsigned long index = fhash(oldinode, olddevice, subj_size);
5201+ struct acl_subject_label **match;
5202+ struct acl_subject_label *tmp;
5203+ __u8 i = 0;
5204+
5205+ match = &s_hash[index];
5206+
5207+ while (*match && ((*match)->inode != oldinode ||
5208+ (*match)->device != olddevice ||
5209+ !((*match)->mode & GR_DELETED))) {
5210+ index = (index + (1 << i)) % subj_size;
5211+ i = (i + 1) % 32;
5212+ match = &s_hash[index];
5213+ }
5214+
5215+ if (*match && (*match != deleted_subject)
5216+ && ((*match)->inode == oldinode)
5217+ && ((*match)->device == olddevice)
5218+ && ((*match)->mode & GR_DELETED)) {
5219+ tmp = *match;
5220+
5221+ tmp->inode = newinode;
5222+ tmp->device = newdevice;
5223+ tmp->mode &= ~GR_DELETED;
5224+
5225+ *match = deleted_subject;
5226+
5227+ insert_acl_subj_label(tmp, role);
5228+ }
5229+
5230+ return;
5231+}
5232+
5233+static __inline__ void
5234+update_inodev_entry(const ino_t oldinode, const kdev_t olddevice,
5235+ const ino_t newinode, const kdev_t newdevice)
5236+{
5237+ unsigned long index = fhash(oldinode, olddevice, inodev_set.n_size);
5238+ struct name_entry **match;
5239+ struct name_entry *tmp;
5240+ __u8 i = 0;
5241+
5242+ match = &inodev_set.n_hash[index];
5243+
5244+ while (*match
5245+ && ((*match)->inode != oldinode
5246+ || (*match)->device != olddevice)) {
5247+ index = (index + (1 << i)) % inodev_set.n_size;
5248+ i = (i + 1) % 32;
5249+ match = &inodev_set.n_hash[index];
5250+ }
5251+
5252+ if (*match && (*match != deleted_inodev)
5253+ && ((*match)->inode == oldinode)
5254+ && ((*match)->device == olddevice)) {
5255+ tmp = *match;
5256+
5257+ tmp->inode = newinode;
5258+ tmp->device = newdevice;
5259+
5260+ *match = deleted_inodev;
5261+
5262+ insert_inodev_entry(tmp);
5263+ }
5264+
5265+ return;
5266+}
5267+
5268+static __inline__ void
5269+do_handle_create(const struct name_entry *matchn, const struct dentry *dentry,
5270+ const struct vfsmount *mnt)
5271+{
5272+ struct acl_subject_label *i;
5273+ struct acl_role_label *role;
5274+
5275+ for (role = role_list_head; role; role = role->next) {
5276+ update_acl_subj_label(matchn->inode, matchn->device,
5277+ dentry->d_inode->i_ino,
5278+ dentry->d_inode->i_dev, role);
5279+
5280+ for (i = role->proc_subject; i; i = i->next) {
5281+ if (unlikely(i->parent_subject &&
5282+ (i->inode == dentry->d_inode->i_ino) &&
5283+ (i->device == dentry->d_inode->i_dev))) {
5284+ i->inode = dentry->d_inode->i_ino;
5285+ i->device = dentry->d_inode->i_dev;
5286+ }
5287+ update_acl_obj_label(matchn->inode, matchn->device,
5288+ dentry->d_inode->i_ino,
5289+ dentry->d_inode->i_dev, i);
5290+ }
5291+ }
5292+
5293+ update_inodev_entry(matchn->inode, matchn->device,
5294+ dentry->d_inode->i_ino, dentry->d_inode->i_dev);
5295+
5296+ return;
5297+}
5298+
5299+void
5300+gr_handle_create(const struct dentry *dentry, const struct vfsmount *mnt)
5301+{
5302+ struct name_entry *matchn;
5303+
5304+ if (unlikely(!(gr_status & GR_READY)))
5305+ return;
5306+
5307+ matchn = lookup_name_entry(gr_to_filename(dentry, mnt));
5308+
5309+ if (unlikely((unsigned long)matchn)) {
5310+ write_lock(&gr_inode_lock);
5311+ do_handle_create(matchn, dentry, mnt);
5312+ write_unlock(&gr_inode_lock);
5313+ }
5314+
5315+ return;
5316+}
5317+
5318+int
5319+gr_handle_rename(struct inode *old_dir, struct inode *new_dir,
5320+ struct dentry *old_dentry,
5321+ struct dentry *new_dentry,
5322+ struct vfsmount *mnt, const __u8 replace)
5323+{
5324+ struct name_entry *matchn;
5325+ int error = 0;
5326+
5327+ matchn = lookup_name_entry(gr_to_filename(new_dentry, mnt));
5328+
5329+ lock_kernel();
5330+ error = vfs_rename(old_dir, old_dentry, new_dir, new_dentry);
5331+ unlock_kernel();
5332+
5333+ if (unlikely(error))
5334+ return error;
5335+
5336+ /* we wouldn't have to check d_inode if it weren't for
5337+ NFS silly-renaming
5338+ */
5339+
5340+ write_lock(&gr_inode_lock);
5341+ if (unlikely(replace && new_dentry->d_inode)) {
5342+ if (unlikely(lookup_inodev_entry(new_dentry->d_inode->i_ino,
5343+ new_dentry->d_inode->i_dev) &&
5344+ (old_dentry->d_inode->i_nlink <= 1)))
5345+ do_handle_delete(new_dentry->d_inode->i_ino,
5346+ new_dentry->d_inode->i_dev);
5347+ }
5348+
5349+ if (unlikely(lookup_inodev_entry(old_dentry->d_inode->i_ino,
5350+ old_dentry->d_inode->i_dev) &&
5351+ (old_dentry->d_inode->i_nlink <= 1)))
5352+ do_handle_delete(old_dentry->d_inode->i_ino,
5353+ old_dentry->d_inode->i_dev);
5354+
5355+ if (unlikely((unsigned long)matchn))
5356+ do_handle_create(matchn, old_dentry, mnt);
5357+ write_unlock(&gr_inode_lock);
5358+
5359+ return error;
5360+}
5361+
5362+static int
5363+lookup_special_role_auth(const char *rolename, unsigned char **salt,
5364+ unsigned char **sum)
5365+{
5366+ struct acl_role_label *r;
5367+ struct role_transition *trans;
5368+ __u16 i;
5369+ int found = 0;
5370+
5371+ /* check transition table */
5372+
5373+ for (trans = current->role->transitions; trans; trans = trans->next) {
5374+ if (!strcmp(rolename, trans->rolename)) {
5375+ found = 1;
5376+ break;
5377+ }
5378+ }
5379+
5380+ if (!found)
5381+ return 0;
5382+
5383+ /* handle special roles that do not require authentication */
5384+
5385+ for (r = role_list_head; r; r = r->next) {
5386+ if (!strcmp(rolename, r->rolename)
5387+ && (r->roletype & GR_ROLE_NOPW)) {
5388+ *salt = NULL;
5389+ *sum = NULL;
5390+ return 1;
5391+ }
5392+ }
5393+
5394+ for (i = 0; i < num_sprole_pws; i++) {
5395+ if (!strcmp(rolename, acl_special_roles[i]->rolename)) {
5396+ *salt = acl_special_roles[i]->salt;
5397+ *sum = acl_special_roles[i]->sum;
5398+ return 1;
5399+ }
5400+ }
5401+
5402+ return 0;
5403+}
5404+
5405+static void
5406+assign_special_role(char *rolename)
5407+{
5408+ struct acl_object_label *obj;
5409+ struct acl_role_label *r;
5410+ struct acl_role_label *assigned = NULL;
5411+ struct task_struct *tsk;
5412+ struct file *filp;
5413+
5414+ for (r = role_list_head; r; r = r->next)
5415+ if (!strcmp(rolename, r->rolename) &&
5416+ (r->roletype & GR_ROLE_SPECIAL))
5417+ assigned = r;
5418+
5419+ if (!assigned)
5420+ return;
5421+
5422+ tsk = current->p_pptr;
5423+ filp = tsk->exec_file;
5424+
5425+ if (tsk && filp) {
5426+ tsk->is_writable = 0;
5427+
5428+ acl_sp_role_value = (acl_sp_role_value % 65535) + 1;
5429+ tsk->acl_sp_role = 1;
5430+ tsk->acl_role_id = acl_sp_role_value;
5431+ tsk->role = assigned;
5432+ tsk->acl =
5433+ chk_subj_label(filp->f_dentry, filp->f_vfsmnt, tsk->role);
5434+
5435+ /* ignore additional mmap checks for processes that are writable
5436+ by the default ACL */
5437+ obj = chk_obj_label(filp->f_dentry, filp->f_vfsmnt, default_role->root_label);
5438+ if (unlikely(obj->mode & GR_WRITE))
5439+ tsk->is_writable = 1;
5440+ obj = chk_obj_label(filp->f_dentry, filp->f_vfsmnt, tsk->role->root_label);
5441+ if (unlikely(obj->mode & GR_WRITE))
5442+ tsk->is_writable = 1;
5443+
5444+#ifdef CONFIG_GRKERNSEC_ACL_DEBUG
5445+ printk(KERN_ALERT "Assigning special role:%s subject:%s to process (%s:%d)\n", tsk->role->rolename, tsk->acl->filename, tsk->comm, tsk->pid);
5446+#endif
5447+ }
5448+
5449+ return;
5450+}
5451+
5452+ssize_t
5453+write_grsec_handler(struct file *file, const char * buf, size_t count, loff_t *ppos)
5454+{
5455+ struct gr_arg *arg;
5456+ struct gr_arg usermode;
5457+ unsigned char *sprole_salt;
5458+ unsigned char *sprole_sum;
5459+ int error = sizeof (struct gr_arg);
5460+ int error2 = 0;
5461+
5462+ spin_lock(&gr_dev_lock);
5463+
5464+ arg = (struct gr_arg *) buf;
5465+
5466+ if (count != sizeof (struct gr_arg)) {
5467+ security_alert_good(GR_DEV_ACL_MSG, count,
5468+ (int) sizeof (struct gr_arg));
5469+ error = -EINVAL;
5470+ goto out;
5471+ }
5472+
5473+ if ((gr_auth_attempts >= CONFIG_GRKERNSEC_ACL_MAXTRIES)
5474+ && time_before_eq(gr_auth_expires, jiffies)) {
5475+ gr_auth_expires = 0;
5476+ gr_auth_attempts = 0;
5477+ }
5478+
5479+ if (copy_from_user(&usermode, arg, sizeof (struct gr_arg))) {
5480+ error = -EFAULT;
5481+ goto out;
5482+ }
5483+
5484+ if (usermode.mode != SPROLE && time_after(gr_auth_expires, jiffies)) {
5485+ error = -EBUSY;
5486+ goto out;
5487+ }
5488+
5489+ /* if non-root trying to do anything other than use a special role,
5490+ do not attempt authentication, do not count towards authentication
5491+ locking
5492+ */
5493+
5494+ if (usermode.mode != SPROLE && current->uid) {
5495+ error = -EPERM;
5496+ goto out;
5497+ }
5498+
5499+ /* ensure pw and special role name are null terminated */
5500+
5501+ usermode.pw[GR_PW_LEN - 1] = '\0';
5502+ usermode.sp_role[GR_SPROLE_LEN - 1] = '\0';
5503+
5504+ /* Okay.
5505+ * We have our enough of the argument structure..(we have yet
5506+ * to copy_from_user the tables themselves) . Copy the tables
5507+ * only if we need them, i.e. for loading operations. */
5508+
5509+ switch (usermode.mode) {
5510+ case SHUTDOWN:
5511+ if ((gr_status & GR_READY)
5512+ && !(chkpw(&usermode, system_salt, system_sum))) {
5513+ gr_status &= ~GR_READY;
5514+ security_alert_good(GR_SHUTS_ACL_MSG, DEFAULTSECARGS);
5515+ free_variables();
5516+ memset(&usermode, 0, sizeof (struct gr_arg));
5517+ memset(&system_salt, 0, sizeof (system_salt));
5518+ memset(&system_sum, 0, sizeof (system_sum));
5519+ } else if (gr_status & GR_READY) {
5520+ security_alert(GR_SHUTF_ACL_MSG, DEFAULTSECARGS);
5521+ error = -EPERM;
5522+ } else {
5523+ security_alert_good(GR_SHUTI_ACL_MSG, DEFAULTSECARGS);
5524+ error = -EAGAIN;
5525+ }
5526+ break;
5527+ case ENABLE:
5528+ if (!(gr_status & GR_READY) && !(error2 = gracl_init(&usermode)))
5529+ security_alert_good(GR_ENABLE_ACL_MSG, GR_VERSION);
5530+ else {
5531+ if (gr_status & GR_READY)
5532+ error = -EAGAIN;
5533+ else
5534+ error = error2;
5535+ security_alert(GR_ENABLEF_ACL_MSG, GR_VERSION,
5536+ DEFAULTSECARGS);
5537+ }
5538+ break;
5539+ case RELOAD:
5540+ if (!(gr_status & GR_READY)) {
5541+ security_alert_good(GR_RELOADI_ACL_MSG);
5542+ error = -EAGAIN;
5543+ } else if (!(chkpw(&usermode, system_salt, system_sum))) {
5544+ gr_status &= ~GR_READY;
5545+ free_variables();
5546+ if (!(error2 = gracl_init(&usermode)))
5547+ security_alert_good(GR_RELOAD_ACL_MSG,
5548+ GR_VERSION);
5549+ else {
5550+ error = error2;
5551+ security_alert(GR_RELOADF_ACL_MSG, GR_VERSION,
5552+ DEFAULTSECARGS);
5553+ }
5554+ } else {
5555+ security_alert(GR_RELOADF_ACL_MSG, GR_VERSION,
5556+ DEFAULTSECARGS);
5557+ error = -EPERM;
5558+ }
5559+ break;
5560+ case SEGVMOD:
5561+ if (unlikely(!(gr_status & GR_READY))) {
5562+ security_alert_good(GR_SEGVMODI_ACL_MSG,
5563+ DEFAULTSECARGS);
5564+ error = -EAGAIN;
5565+ break;
5566+ }
5567+
5568+ if (!(chkpw(&usermode, system_salt, system_sum))) {
5569+ security_alert_good(GR_SEGVMODS_ACL_MSG,
5570+ DEFAULTSECARGS);
5571+ if (usermode.segv_device && usermode.segv_inode) {
5572+ struct acl_subject_label *segvacl;
5573+ segvacl =
5574+ lookup_acl_subj_label(usermode.segv_inode,
5575+ usermode.segv_device,
5576+ current->role);
5577+ if (segvacl) {
5578+ segvacl->crashes = 0;
5579+ segvacl->expires = 0;
5580+ }
5581+ } else if (gr_find_uid(usermode.segv_uid) >= 0) {
5582+ gr_remove_uid(usermode.segv_uid);
5583+ }
5584+ } else {
5585+ security_alert(GR_SEGVMODF_ACL_MSG, DEFAULTSECARGS);
5586+ error = -EPERM;
5587+ }
5588+ break;
5589+ case SPROLE:
5590+ if (unlikely(!(gr_status & GR_READY))) {
5591+ security_alert_good(GR_SPROLEI_ACL_MSG, DEFAULTSECARGS);
5592+ error = -EAGAIN;
5593+ break;
5594+ }
5595+
5596+ if ((current->role->auth_attempts >= CONFIG_GRKERNSEC_ACL_MAXTRIES)
5597+ && time_before_eq(current->role->expires, jiffies)) {
5598+ current->role->expires = 0;
5599+ current->role->auth_attempts = 0;
5600+ }
5601+
5602+ if (time_after(current->role->expires, jiffies)) {
5603+ error = -EBUSY;
5604+ goto out;
5605+ }
5606+
5607+ if (lookup_special_role_auth
5608+ (usermode.sp_role, &sprole_salt, &sprole_sum)
5609+ && ((!sprole_salt && !sprole_sum)
5610+ || !(chkpw(&usermode, sprole_salt, sprole_sum)))) {
5611+ assign_special_role(usermode.sp_role);
5612+ security_alert_good(GR_SPROLES_ACL_MSG,
5613+ (current->p_pptr) ? current->
5614+ p_pptr->role->rolename : "",
5615+ acl_sp_role_value, DEFAULTSECARGS);
5616+ } else {
5617+ security_alert(GR_SPROLEF_ACL_MSG, usermode.sp_role,
5618+ DEFAULTSECARGS);
5619+ error = -EPERM;
5620+ current->role->auth_attempts++;
5621+ if (current->role->auth_attempts >= CONFIG_GRKERNSEC_ACL_MAXTRIES) {
5622+ current->role->expires =
5623+ jiffies + CONFIG_GRKERNSEC_ACL_TIMEOUT * HZ;
5624+ security_alert(GR_MAXROLEPW_ACL_MSG,
5625+ CONFIG_GRKERNSEC_ACL_MAXTRIES,
5626+ usermode.sp_role, DEFAULTSECARGS);
5627+ }
5628+
5629+ goto out;
5630+ }
5631+ break;
5632+ default:
5633+ security_alert(GR_INVMODE_ACL_MSG, usermode.mode,
5634+ DEFAULTSECARGS);
5635+ error = -EINVAL;
5636+ break;
5637+ }
5638+
5639+ if (error != -EPERM)
5640+ goto out;
5641+
5642+ gr_auth_attempts++;
5643+
5644+ if (gr_auth_attempts >= CONFIG_GRKERNSEC_ACL_MAXTRIES) {
5645+ security_alert(GR_MAXPW_ACL_MSG, CONFIG_GRKERNSEC_ACL_MAXTRIES);
5646+ gr_auth_expires = jiffies + CONFIG_GRKERNSEC_ACL_TIMEOUT * HZ;
5647+ }
5648+
5649+ out:
5650+ spin_unlock(&gr_dev_lock);
5651+ return error;
5652+}
5653+
5654+int
5655+gr_set_acls(const int type)
5656+{
5657+ struct acl_object_label *obj;
5658+ struct task_struct *task;
5659+ struct file *filp;
5660+ unsigned short i;
5661+
5662+ read_lock(&tasklist_lock);
5663+ for_each_task(task) {
5664+ /* check to see if we're called from the exit handler,
5665+ if so, only replace ACLs that have inherited the admin
5666+ ACL */
5667+
5668+ if (type && (task->role != current->role ||
5669+ task->acl_role_id != current->acl_role_id))
5670+ continue;
5671+
5672+ task->acl_role_id = 0;
5673+
5674+ if ((filp = task->exec_file)) {
5675+ do_set_role_label(task, task->uid, task->gid);
5676+
5677+ task->acl =
5678+ chk_subj_label(filp->f_dentry, filp->f_vfsmnt,
5679+ task->role);
5680+ if (task->acl) {
5681+ struct acl_subject_label *curr;
5682+ curr = task->acl;
5683+
5684+ task->is_writable = 0;
5685+ /* ignore additional mmap checks for processes that are writable
5686+ by the default ACL */
5687+ obj = chk_obj_label(filp->f_dentry, filp->f_vfsmnt, default_role->root_label);
5688+ if (unlikely(obj->mode & GR_WRITE))
5689+ task->is_writable = 1;
5690+ obj = chk_obj_label(filp->f_dentry, filp->f_vfsmnt, task->role->root_label);
5691+ if (unlikely(obj->mode & GR_WRITE))
5692+ task->is_writable = 1;
5693+
5694+#ifdef CONFIG_GRKERNSEC_ACL_DEBUG
5695+ printk(KERN_ALERT "gr_set_acls for (%s:%d): role:%s, subject:%s\n", task->comm, task->pid, task->role->rolename, task->acl->filename);
5696+#endif
5697+ if (!(curr->mode & GR_LEARN))
5698+ for (i = 0; i < RLIM_NLIMITS; i++) {
5699+ if (!(curr->resmask & (1 << i)))
5700+ continue;
5701+
5702+ task->rlim[i].rlim_cur =
5703+ curr->res[i].rlim_cur;
5704+ task->rlim[i].rlim_max =
5705+ curr->res[i].rlim_max;
5706+ }
5707+ } else {
5708+ read_unlock(&tasklist_lock);
5709+ security_alert_good(GR_DEFACL_MSG, task->comm,
5710+ task->pid);
5711+ return 1;
5712+ }
5713+ } else {
5714+ // it's a kernel process
5715+ task->role = kernel_role;
5716+ task->acl = kernel_role->root_label;
5717+#ifdef CONFIG_GRKERNSEC_ACL_HIDEKERN
5718+ task->acl->mode &= ~GR_FIND;
5719+#endif
5720+ }
5721+ }
5722+ read_unlock(&tasklist_lock);
5723+ return 0;
5724+}
5725+
5726+void
5727+gr_learn_resource(const struct task_struct *task,
5728+ const int res, const unsigned long wanted)
5729+{
5730+ struct acl_subject_label *acl;
5731+
5732+ if (unlikely((gr_status & GR_READY) &&
5733+ task->acl && (task->acl->mode & GR_LEARN)))
5734+ goto skip_reslog;
5735+
5736+#ifdef CONFIG_GRKERNSEC_RESLOG
5737+ gr_log_resource(task, res, wanted);
5738+#endif
5739+ skip_reslog:
5740+
5741+ if (unlikely(!(gr_status & GR_READY) || !wanted))
5742+ return;
5743+
5744+ acl = task->acl;
5745+
5746+ if (likely(!acl || !(acl->mode & GR_LEARN) ||
5747+ !(acl->resmask & (1 << (unsigned short) res))))
5748+ return;
5749+
5750+ if (wanted >= acl->res[res].rlim_cur) {
5751+ unsigned long res_add;
5752+
5753+ res_add = wanted;
5754+ switch (res) {
5755+ case RLIMIT_CPU:
5756+ res_add += GR_RLIM_CPU_BUMP;
5757+ break;
5758+ case RLIMIT_FSIZE:
5759+ res_add += GR_RLIM_FSIZE_BUMP;
5760+ break;
5761+ case RLIMIT_DATA:
5762+ res_add += GR_RLIM_DATA_BUMP;
5763+ break;
5764+ case RLIMIT_STACK:
5765+ res_add += GR_RLIM_STACK_BUMP;
5766+ break;
5767+ case RLIMIT_CORE:
5768+ res_add += GR_RLIM_CORE_BUMP;
5769+ break;
5770+ case RLIMIT_RSS:
5771+ res_add += GR_RLIM_RSS_BUMP;
5772+ break;
5773+ case RLIMIT_NPROC:
5774+ res_add += GR_RLIM_NPROC_BUMP;
5775+ break;
5776+ case RLIMIT_NOFILE:
5777+ res_add += GR_RLIM_NOFILE_BUMP;
5778+ break;
5779+ case RLIMIT_MEMLOCK:
5780+ res_add += GR_RLIM_MEMLOCK_BUMP;
5781+ break;
5782+ case RLIMIT_AS:
5783+ res_add += GR_RLIM_AS_BUMP;
5784+ break;
5785+ case RLIMIT_LOCKS:
5786+ res_add += GR_RLIM_LOCKS_BUMP;
5787+ break;
5788+ }
5789+
5790+ acl->res[res].rlim_cur = res_add;
5791+
5792+ if (wanted > acl->res[res].rlim_max)
5793+ acl->res[res].rlim_max = res_add;
5794+
5795+ security_learn(GR_LEARN_AUDIT_MSG, current->role->rolename,
5796+ current->role->roletype, acl->filename,
5797+ acl->res[res].rlim_cur, acl->res[res].rlim_max,
5798+ "", (unsigned long) res);
5799+ }
5800+
5801+ return;
5802+}
5803+
5804+#ifdef CONFIG_SYSCTL
5805+extern struct proc_dir_entry *proc_sys_root;
5806+
5807+__u32
5808+gr_handle_sysctl(const struct ctl_table *table, const void *oldval,
5809+ const void *newval)
5810+{
5811+ struct proc_dir_entry *tmp;
5812+ struct nameidata nd;
5813+ const char *proc_sys = "/proc/sys";
5814+ char *path = gr_shared_page[0][smp_processor_id()];
5815+ struct acl_object_label *obj;
5816+ unsigned short len = 0, pos = 0, depth = 0, i;
5817+ __u32 err = 0;
5818+ __u32 mode = 0;
5819+
5820+ if (unlikely(!(gr_status & GR_READY)))
5821+ return 1;
5822+
5823+ if (oldval)
5824+ mode |= GR_READ;
5825+ if (newval)
5826+ mode |= GR_WRITE;
5827+
5828+ /* convert the requested sysctl entry into a pathname */
5829+
5830+ for (tmp = table->de; tmp != proc_sys_root; tmp = tmp->parent) {
5831+ len += strlen(tmp->name);
5832+ len++;
5833+ depth++;
5834+ }
5835+
5836+ if ((len + depth + strlen(proc_sys) + 1) > PAGE_SIZE)
5837+ return 0; // deny
5838+
5839+ memset(path, 0, PAGE_SIZE);
5840+
5841+ memcpy(path, proc_sys, strlen(proc_sys));
5842+
5843+ pos += strlen(proc_sys);
5844+
5845+ for (; depth > 0; depth--) {
5846+ path[pos] = '/';
5847+ pos++;
5848+ for (i = 1, tmp = table->de; tmp != proc_sys_root;
5849+ tmp = tmp->parent) {
5850+ if (depth == i) {
5851+ memcpy(path + pos, tmp->name,
5852+ strlen(tmp->name));
5853+ pos += strlen(tmp->name);
5854+ }
5855+ i++;
5856+ }
5857+ }
5858+
5859+ if (path_init(path, LOOKUP_FOLLOW, &nd))
5860+ err = path_walk(path, &nd);
5861+
5862+ if (err)
5863+ goto out;
5864+
5865+ obj = chk_obj_label(nd.dentry, nd.mnt, current->acl);
5866+ err = obj->mode & (mode | to_gr_audit(mode) | GR_SUPPRESS);
5867+
5868+ if (unlikely((current->acl->mode & GR_LEARN) && ((err & mode) != mode))) {
5869+ __u32 new_mode = mode;
5870+
5871+ new_mode &= ~(GR_AUDITS | GR_SUPPRESS);
5872+
5873+ err = new_mode;
5874+ gr_log_learn(current->role, current->uid, current->gid,
5875+ current, path, new_mode);
5876+ } else if ((err & mode) != mode && !(err & GR_SUPPRESS)) {
5877+ security_alert(GR_SYSCTL_ACL_MSG, "denied", path,
5878+ (mode & GR_READ) ? " reading" : "",
5879+ (mode & GR_WRITE) ? " writing" : "",
5880+ DEFAULTSECARGS);
5881+ err = 0;
5882+ } else if ((err & mode) != mode) {
5883+ err = 0;
5884+ } else if (((err & mode) == mode) && (err & GR_AUDITS)) {
5885+ security_audit(GR_SYSCTL_ACL_MSG, "successful",
5886+ path, (mode & GR_READ) ? " reading" : "",
5887+ (mode & GR_WRITE) ? " writing" : "",
5888+ DEFAULTSECARGS);
5889+ }
5890+
5891+ path_release(&nd);
5892+
5893+ out:
5894+ return err;
5895+}
5896+#endif
5897+
5898+int
5899+gr_handle_ptrace(struct task_struct *task, const long request)
5900+{
5901+ struct file *filp;
5902+ __u32 retmode;
5903+
5904+ if (unlikely(!(gr_status & GR_READY)))
5905+ return 0;
5906+
5907+ filp = task->exec_file;
5908+
5909+ if (unlikely(!filp))
5910+ return 0;
5911+
5912+ retmode = gr_search_file(filp->f_dentry, GR_PTRACERD, filp->f_vfsmnt);
5913+
5914+ if (retmode & GR_PTRACERD) {
5915+ switch (request) {
5916+ case PTRACE_POKETEXT:
5917+ case PTRACE_POKEDATA:
5918+ case PTRACE_POKEUSR:
5919+#if !defined(CONFIG_PPC32) && !defined(CONFIG_PARISC) && !defined(CONFIG_ALPHA)
5920+ case PTRACE_SETREGS:
5921+ case PTRACE_SETFPREGS:
5922+#endif
5923+#ifdef CONFIG_X86
5924+ case PTRACE_SETFPXREGS:
5925+#endif
5926+#ifdef CONFIG_ALTIVEC
5927+ case PTRACE_SETVRREGS:
5928+#endif
5929+ return 1;
5930+ default:
5931+ return 0;
5932+ }
5933+ } else if (!(current->acl->mode & GR_OVERRIDE) &&
5934+ !(current->role->roletype & GR_ROLE_GOD)
5935+ && (current->acl != task->acl
5936+ || (current->acl != current->role->root_label
5937+ && current->pid != task->pid))) {
5938+ security_alert(GR_PTRACE_ACL_MSG,
5939+ gr_to_filename(filp->f_dentry, filp->f_vfsmnt),
5940+ task->comm, task->pid, DEFAULTSECARGS);
5941+ return 1;
5942+ }
5943+
5944+ return 0;
5945+}
5946+
5947+int
5948+gr_handle_ptrace_exec(const struct dentry *dentry, const struct vfsmount *mnt)
5949+{
5950+ __u32 retmode;
5951+ struct acl_subject_label *subj;
5952+
5953+ if (unlikely(!(gr_status & GR_READY)))
5954+ return 0;
5955+
5956+ if (unlikely
5957+ ((current->ptrace & PT_PTRACED)
5958+ && !(current->acl->mode & GR_OVERRIDE)))
5959+ retmode = gr_search_file(dentry, GR_PTRACERD, mnt);
5960+ else
5961+ return 0;
5962+
5963+ subj = chk_subj_label(dentry, mnt, current->role);
5964+
5965+ if (!(retmode & GR_PTRACERD) &&
5966+ !(current->role->roletype & GR_ROLE_GOD) &&
5967+ (current->acl != subj)) {
5968+ security_alert(GR_PTRACE_EXEC_ACL_MSG,
5969+ gr_to_filename(dentry, mnt), DEFAULTSECARGS);
5970+ return 1;
5971+ }
5972+
5973+ return 0;
5974+}
5975+
5976+int
5977+gr_handle_mmap(const struct file *filp, const unsigned long prot)
5978+{
5979+ struct acl_object_label *obj, *obj2;
5980+
5981+ if (unlikely(!(gr_status & GR_READY) ||
5982+ (current->acl->mode & GR_OVERRIDE) || !filp ||
5983+ !(prot & PROT_EXEC)))
5984+ return 0;
5985+
5986+ if (unlikely(current->is_writable))
5987+ return 0;
5988+
5989+ obj = chk_obj_label(filp->f_dentry, filp->f_vfsmnt, default_role->root_label);
5990+ obj2 = chk_obj_label(filp->f_dentry, filp->f_vfsmnt,
5991+ current->role->root_label);
5992+ if (unlikely((obj->mode & GR_WRITE) || (obj2->mode & GR_WRITE))) {
5993+ security_alert(GR_WRITLIB_ACL_MSG,
5994+ gr_to_filename(filp->f_dentry, filp->f_vfsmnt),
5995+ DEFAULTSECARGS);
5996+ return 1;
5997+ }
5998+
5999+ return 0;
6000+}
6001+
6002+int
6003+gr_acl_handle_mmap(const struct file *file, const unsigned long prot)
6004+{
6005+ __u32 mode;
6006+
6007+ if (unlikely(!file || !(prot & PROT_EXEC)))
6008+ return 1;
6009+
6010+ mode =
6011+ gr_search_file(file->f_dentry,
6012+ GR_EXEC | GR_AUDIT_EXEC | GR_SUPPRESS,
6013+ file->f_vfsmnt);
6014+
6015+ if (unlikely(!gr_tpe_allow(file) || (!(mode & GR_EXEC) && !(mode & GR_SUPPRESS)))) {
6016+ security_alert(GR_MMAP_ACL_MSG, "denied",
6017+ gr_to_filename(file->f_dentry, file->f_vfsmnt),
6018+ DEFAULTSECARGS);
6019+ return 0;
6020+ } else if (unlikely(!gr_tpe_allow(file) || !(mode & GR_EXEC))) {
6021+ return 0;
6022+ } else if (unlikely(mode & GR_EXEC && mode & GR_AUDIT_EXEC)) {
6023+ security_audit(GR_MMAP_ACL_MSG, "successful",
6024+ gr_to_filename(file->f_dentry, file->f_vfsmnt),
6025+ DEFAULTSECARGS);
6026+ return 1;
6027+ }
6028+
6029+ return 1;
6030+}
6031+
6032+int
6033+gr_acl_handle_mprotect(const struct file *file, const unsigned long prot)
6034+{
6035+ __u32 mode;
6036+
6037+ if (unlikely(!file || !(prot & PROT_EXEC)))
6038+ return 1;
6039+
6040+ mode =
6041+ gr_search_file(file->f_dentry,
6042+ GR_EXEC | GR_AUDIT_EXEC | GR_SUPPRESS,
6043+ file->f_vfsmnt);
6044+
6045+ if (unlikely(!gr_tpe_allow(file) || (!(mode & GR_EXEC) && !(mode & GR_SUPPRESS)))) {
6046+ security_alert(GR_MPROTECT_ACL_MSG, "denied",
6047+ gr_to_filename(file->f_dentry, file->f_vfsmnt),
6048+ DEFAULTSECARGS);
6049+ return 0;
6050+ } else if (unlikely(!gr_tpe_allow(file) || !(mode & GR_EXEC))) {
6051+ return 0;
6052+ } else if (unlikely(mode & GR_EXEC && mode & GR_AUDIT_EXEC)) {
6053+ security_audit(GR_MPROTECT_ACL_MSG, "successful",
6054+ gr_to_filename(file->f_dentry, file->f_vfsmnt),
6055+ DEFAULTSECARGS);
6056+ return 1;
6057+ }
6058+
6059+ return 1;
6060+}
6061+
6062+void
6063+gr_acl_handle_psacct(struct task_struct *task, const long code)
6064+{
6065+ unsigned long runtime;
6066+ unsigned long cputime;
6067+ unsigned int wday, cday;
6068+ __u8 whr, chr;
6069+ __u8 wmin, cmin;
6070+ __u8 wsec, csec;
6071+ char cur_tty[64] = { 0 };
6072+ char parent_tty[64] = { 0 };
6073+
6074+ if (unlikely(!(gr_status & GR_READY) || !task->acl ||
6075+ !(task->acl->mode & GR_PROCACCT)))
6076+ return;
6077+
6078+ runtime = (jiffies - task->start_time) / HZ;
6079+ wday = runtime / (3600 * 24);
6080+ runtime -= wday * (3600 * 24);
6081+ whr = runtime / 3600;
6082+ runtime -= whr * 3600;
6083+ wmin = runtime / 60;
6084+ runtime -= wmin * 60;
6085+ wsec = runtime;
6086+
6087+ cputime = (task->times.tms_utime + task->times.tms_stime) / HZ;
6088+ cday = cputime / (3600 * 24);
6089+ cputime -= cday * (3600 * 24);
6090+ chr = cputime / 3600;
6091+ cputime -= chr * 3600;
6092+ cmin = cputime / 60;
6093+ cputime -= cmin * 60;
6094+ csec = cputime;
6095+
6096+ security_audit(GR_ACL_PROCACCT_MSG, task->comm,
6097+ task->pid, NIPQUAD(task->curr_ip), tty_name(task->tty,
6098+ cur_tty),
6099+ task->uid, task->euid, task->gid, task->egid, wday, whr,
6100+ wmin, wsec, cday, chr, cmin, csec,
6101+ (task->
6102+ flags & PF_SIGNALED) ? "killed by signal" : "exited",
6103+ code, task->p_pptr->comm, task->p_pptr->pid,
6104+ NIPQUAD(task->p_pptr->curr_ip),
6105+ tty_name(task->p_pptr->tty, parent_tty),
6106+ task->p_pptr->uid, task->p_pptr->euid, task->p_pptr->gid,
6107+ task->p_pptr->egid);
6108+
6109+ return;
6110+}
6111+
6112+void gr_set_kernel_label(struct task_struct *task)
6113+{
6114+ if (gr_status & GR_READY) {
6115+ task->role = kernel_role;
6116+ task->acl = kernel_role->root_label;
6117+ }
6118+ return;
6119+}
6120diff -urN linux-2.4.21/grsecurity/gracl_alloc.c linux-2.4.21/grsecurity/gracl_alloc.c
6121--- linux-2.4.21/grsecurity/gracl_alloc.c 1969-12-31 19:00:00.000000000 -0500
6122+++ linux-2.4.21/grsecurity/gracl_alloc.c 2003-06-23 11:49:16.000000000 -0400
6123@@ -0,0 +1,94 @@
6124+/* stack-based acl allocation tracking (c) Brad Spengler 2002,2003 */
6125+
6126+#include <linux/kernel.h>
6127+#include <linux/mm.h>
6128+#include <linux/slab.h>
6129+#include <linux/vmalloc.h>
6130+#include <linux/gracl.h>
6131+#include <linux/grsecurity.h>
6132+
6133+static unsigned long alloc_stack_next = 1;
6134+static unsigned long alloc_stack_size = 1;
6135+static void **alloc_stack;
6136+
6137+static __inline__ int
6138+alloc_pop(void)
6139+{
6140+ if ((alloc_stack_next - 1) == 0)
6141+ return 0;
6142+
6143+ if (*(alloc_stack + alloc_stack_next - 2))
6144+ kfree(*(alloc_stack + alloc_stack_next - 2));
6145+
6146+ alloc_stack_next--;
6147+
6148+ return 1;
6149+}
6150+
6151+static __inline__ void
6152+alloc_push(void *buf)
6153+{
6154+ if (alloc_stack_next >= alloc_stack_size)
6155+ BUG();
6156+
6157+ *(alloc_stack + alloc_stack_next - 1) = buf;
6158+
6159+ alloc_stack_next++;
6160+
6161+ return;
6162+}
6163+
6164+void *
6165+acl_alloc(unsigned long len)
6166+{
6167+ void *ret;
6168+
6169+ if (len > PAGE_SIZE)
6170+ BUG();
6171+
6172+ ret = kmalloc(len, GFP_KERNEL);
6173+
6174+ if (ret)
6175+ alloc_push(ret);
6176+
6177+ return ret;
6178+}
6179+
6180+void
6181+acl_free_all(void)
6182+{
6183+ if (gr_acl_is_enabled() || !alloc_stack)
6184+ return;
6185+
6186+ while (alloc_pop()) ;
6187+
6188+ if (alloc_stack) {
6189+ if ((alloc_stack_size * sizeof (void *)) <= PAGE_SIZE)
6190+ kfree(alloc_stack);
6191+ else
6192+ vfree(alloc_stack);
6193+ }
6194+
6195+ alloc_stack = NULL;
6196+ alloc_stack_size = 1;
6197+ alloc_stack_next = 1;
6198+
6199+ return;
6200+}
6201+
6202+int
6203+acl_alloc_stack_init(unsigned long size)
6204+{
6205+ if ((size * sizeof (void *)) <= PAGE_SIZE)
6206+ alloc_stack =
6207+ (void **) kmalloc(size * sizeof (void *), GFP_KERNEL);
6208+ else
6209+ alloc_stack = (void **) vmalloc(size * sizeof (void *));
6210+
6211+ alloc_stack_size = size;
6212+
6213+ if (!alloc_stack)
6214+ return 0;
6215+ else
6216+ return 1;
6217+}
6218diff -urN linux-2.4.21/grsecurity/gracl_cap.c linux-2.4.21/grsecurity/gracl_cap.c
6219--- linux-2.4.21/grsecurity/gracl_cap.c 1969-12-31 19:00:00.000000000 -0500
6220+++ linux-2.4.21/grsecurity/gracl_cap.c 2003-06-23 11:49:16.000000000 -0400
6221@@ -0,0 +1,71 @@
6222+/* capability handling routines, (c) Brad Spengler 2002,2003 */
6223+
6224+#include <linux/kernel.h>
6225+#include <linux/sched.h>
6226+#include <linux/capability.h>
6227+#include <linux/gracl.h>
6228+#include <linux/grsecurity.h>
6229+#include <linux/grinternal.h>
6230+
6231+static const char *captab_log[29] = {
6232+ "CAP_CHOWN",
6233+ "CAP_DAC_OVERRIDE",
6234+ "CAP_DAC_READ_SEARCH",
6235+ "CAP_FOWNER",
6236+ "CAP_FSETID",
6237+ "CAP_KILL",
6238+ "CAP_SETGID",
6239+ "CAP_SETUID",
6240+ "CAP_SETPCAP",
6241+ "CAP_LINUX_IMMUTABLE",
6242+ "CAP_NET_BIND_SERVICE",
6243+ "CAP_NET_BROADCAST",
6244+ "CAP_NET_ADMIN",
6245+ "CAP_NET_RAW",
6246+ "CAP_IPC_LOCK",
6247+ "CAP_IPC_OWNER",
6248+ "CAP_SYS_MODULE",
6249+ "CAP_SYS_RAWIO",
6250+ "CAP_SYS_CHROOT",
6251+ "CAP_SYS_PTRACE",
6252+ "CAP_SYS_PACCT",
6253+ "CAP_SYS_ADMIN",
6254+ "CAP_SYS_BOOT",
6255+ "CAP_SYS_NICE",
6256+ "CAP_SYS_RESOURCE",
6257+ "CAP_SYS_TIME",
6258+ "CAP_SYS_TTY_CONFIG",
6259+ "CAP_MKNOD",
6260+ "CAP_LEASE"
6261+};
6262+
6263+int
6264+gr_is_capable(const int cap)
6265+{
6266+ struct acl_subject_label *curracl;
6267+
6268+ if (!gr_acl_is_enabled())
6269+ return 1;
6270+
6271+ curracl = current->acl;
6272+
6273+ if (!cap_raised(curracl->cap_lower, cap))
6274+ return 1;
6275+
6276+ if ((curracl->mode & GR_LEARN)
6277+ && cap_raised(current->cap_effective, cap)) {
6278+ security_learn(GR_LEARN_AUDIT_MSG, current->role->rolename,
6279+ current->role->roletype, current->uid,
6280+ current->gid, current->exec_file ?
6281+ gr_to_filename(current->exec_file->f_dentry,
6282+ current->exec_file->f_vfsmnt) : curracl->filename,
6283+ curracl->filename, 0UL,
6284+ 0UL, "", (unsigned long) cap, NIPQUAD(current->curr_ip));
6285+ return 1;
6286+ }
6287+
6288+ if ((cap >= 0) && (cap < 29) && cap_raised(current->cap_effective, cap))
6289+ security_alert(GR_CAP_ACL_MSG, captab_log[cap], DEFAULTSECARGS);
6290+
6291+ return 0;
6292+}
6293diff -urN linux-2.4.21/grsecurity/gracl_fs.c linux-2.4.21/grsecurity/gracl_fs.c
6294--- linux-2.4.21/grsecurity/gracl_fs.c 1969-12-31 19:00:00.000000000 -0500
6295+++ linux-2.4.21/grsecurity/gracl_fs.c 2003-06-23 11:49:16.000000000 -0400
6296@@ -0,0 +1,469 @@
6297+#include <linux/kernel.h>
6298+#include <linux/sched.h>
6299+#include <linux/types.h>
6300+#include <linux/fs.h>
6301+#include <linux/file.h>
6302+#include <linux/grsecurity.h>
6303+#include <linux/grinternal.h>
6304+#include <linux/gracl.h>
6305+
6306+__u32
6307+gr_acl_handle_hidden_file(const struct dentry * dentry,
6308+ const struct vfsmount * mnt)
6309+{
6310+ __u32 mode;
6311+
6312+ if (unlikely(!dentry->d_inode))
6313+ return GR_FIND;
6314+
6315+ mode =
6316+ gr_search_file(dentry, GR_FIND | GR_AUDIT_FIND | GR_SUPPRESS, mnt);
6317+
6318+ if (unlikely(mode & GR_FIND && mode & GR_AUDIT_FIND)) {
6319+ security_audit(GR_HIDDEN_ACL_MSG, "successful",
6320+ gr_to_filename(dentry, mnt), DEFAULTSECARGS);
6321+ return mode;
6322+ } else if (unlikely(!(mode & GR_FIND) && !(mode & GR_SUPPRESS))) {
6323+ security_alert(GR_HIDDEN_ACL_MSG, "denied",
6324+ gr_to_filename(dentry, mnt),
6325+ DEFAULTSECARGS);
6326+ return 0;
6327+ } else if (unlikely(!(mode & GR_FIND)))
6328+ return 0;
6329+
6330+ return GR_FIND;
6331+}
6332+
6333+__u32
6334+gr_acl_handle_open(const struct dentry * dentry, const struct vfsmount * mnt,
6335+ const int fmode)
6336+{
6337+ __u32 reqmode = GR_FIND;
6338+ __u32 mode;
6339+
6340+ if (unlikely(!dentry->d_inode))
6341+ return reqmode;
6342+
6343+ if (unlikely(fmode & O_APPEND))
6344+ reqmode |= GR_APPEND;
6345+ else if (unlikely(fmode & FMODE_WRITE))
6346+ reqmode |= GR_WRITE;
6347+ if (likely((fmode & FMODE_READ) && !(fmode & O_DIRECTORY)))
6348+ reqmode |= GR_READ;
6349+
6350+ mode =
6351+ gr_search_file(dentry, reqmode | to_gr_audit(reqmode) | GR_SUPPRESS,
6352+ mnt);
6353+
6354+ if (unlikely(((mode & reqmode) == reqmode) && mode & GR_AUDITS)) {
6355+ security_audit(GR_OPEN_ACL_MSG, "successful",
6356+ gr_to_filename(dentry, mnt),
6357+ reqmode & GR_READ ? " reading" : "",
6358+ reqmode & GR_WRITE ? " writing" :
6359+ reqmode & GR_APPEND ? " appending" : "",
6360+ DEFAULTSECARGS);
6361+ return reqmode;
6362+ } else
6363+ if (unlikely((mode & reqmode) != reqmode && !(mode & GR_SUPPRESS)))
6364+ {
6365+ security_alert(GR_OPEN_ACL_MSG, "denied",
6366+ gr_to_filename(dentry, mnt),
6367+ reqmode & GR_READ ? " reading" : "",
6368+ reqmode & GR_WRITE ? " writing" : reqmode &
6369+ GR_APPEND ? " appending" : "", DEFAULTSECARGS);
6370+ return 0;
6371+ } else if (unlikely((mode & reqmode) != reqmode))
6372+ return 0;
6373+
6374+ return reqmode;
6375+}
6376+
6377+__u32
6378+gr_acl_handle_creat(const struct dentry * dentry,
6379+ const struct dentry * p_dentry,
6380+ const struct vfsmount * p_mnt, const int fmode,
6381+ const int imode)
6382+{
6383+ __u32 reqmode = GR_WRITE | GR_CREATE;
6384+ __u32 mode;
6385+
6386+ if (unlikely(fmode & O_APPEND))
6387+ reqmode |= GR_APPEND;
6388+ if (unlikely((fmode & FMODE_READ) && !(fmode & O_DIRECTORY)))
6389+ reqmode |= GR_READ;
6390+ if (unlikely((fmode & O_CREAT) && (imode & (S_ISUID | S_ISGID))))
6391+ reqmode |= GR_SETID;
6392+
6393+ mode =
6394+ gr_check_create(dentry, p_dentry, p_mnt,
6395+ reqmode | to_gr_audit(reqmode) | GR_SUPPRESS);
6396+
6397+ if (unlikely(((mode & reqmode) == reqmode) && mode & GR_AUDITS)) {
6398+ security_audit(GR_CREATE_ACL_MSG, "successful",
6399+ gr_to_filename(dentry, p_mnt),
6400+ reqmode & GR_READ ? " reading" : "",
6401+ reqmode & GR_WRITE ? " writing" :
6402+ reqmode & GR_APPEND ? " appending" : "",
6403+ DEFAULTSECARGS);
6404+ return reqmode;
6405+ } else
6406+ if (unlikely((mode & reqmode) != reqmode && !(mode & GR_SUPPRESS)))
6407+ {
6408+ security_alert(GR_CREATE_ACL_MSG, "denied",
6409+ gr_to_filename(dentry, p_mnt),
6410+ reqmode & GR_READ ? " reading" : "",
6411+ reqmode & GR_WRITE ? " writing" : reqmode &
6412+ GR_APPEND ? " appending" : "", DEFAULTSECARGS);
6413+ return 0;
6414+ } else if (unlikely((mode & reqmode) != reqmode))
6415+ return 0;
6416+
6417+ return reqmode;
6418+}
6419+
6420+__u32
6421+gr_acl_handle_access(const struct dentry * dentry, const struct vfsmount * mnt,
6422+ const int fmode)
6423+{
6424+ __u32 mode, reqmode = GR_FIND;
6425+
6426+ if ((fmode & S_IXOTH) && !S_ISDIR(dentry->d_inode->i_mode))
6427+ reqmode |= GR_EXEC;
6428+ if (fmode & S_IWOTH)
6429+ reqmode |= GR_WRITE;
6430+ if (fmode & S_IROTH)
6431+ reqmode |= GR_READ;
6432+
6433+ mode =
6434+ gr_search_file(dentry, reqmode | to_gr_audit(reqmode) | GR_SUPPRESS,
6435+ mnt);
6436+
6437+ if (unlikely(((mode & reqmode) == reqmode) && mode & GR_AUDITS)) {
6438+ security_audit(GR_ACCESS_ACL_MSG, "successful",
6439+ gr_to_filename(dentry, mnt),
6440+ reqmode & GR_READ ? " reading" : "",
6441+ reqmode & GR_WRITE ? " writing" : "",
6442+ reqmode & GR_EXEC ? " executing" : "",
6443+ DEFAULTSECARGS);
6444+ return reqmode;
6445+ } else
6446+ if (unlikely((mode & reqmode) != reqmode && !(mode & GR_SUPPRESS)))
6447+ {
6448+ security_alert(GR_ACCESS_ACL_MSG, "denied",
6449+ gr_to_filename(dentry, mnt),
6450+ reqmode & GR_READ ? " reading" : "",
6451+ reqmode & GR_WRITE ? " writing" : "",
6452+ reqmode & GR_EXEC ? " executing" : "",
6453+ DEFAULTSECARGS);
6454+ return 0;
6455+ } else if (unlikely((mode & reqmode) != reqmode))
6456+ return 0;
6457+
6458+ return reqmode;
6459+}
6460+
6461+#define generic_fs_handler(dentry, mnt, reqmode, fmt) \
6462+{ \
6463+ __u32 mode; \
6464+ \
6465+ mode = gr_search_file(dentry, reqmode | to_gr_audit(reqmode) | GR_SUPPRESS, mnt); \
6466+ \
6467+ if (unlikely(((mode & (reqmode)) == (reqmode)) && mode & GR_AUDITS)) { \
6468+ security_audit(fmt, "successful", \
6469+ gr_to_filename(dentry, mnt), DEFAULTSECARGS); \
6470+ return mode; \
6471+ } else if (unlikely((mode & (reqmode)) != (reqmode) && !(mode & GR_SUPPRESS))) { \
6472+ security_alert(fmt, "denied", gr_to_filename(dentry, mnt), \
6473+ DEFAULTSECARGS); \
6474+ return 0; \
6475+ } else if (unlikely((mode & (reqmode)) != (reqmode))) \
6476+ return 0; \
6477+ \
6478+ return (reqmode); \
6479+}
6480+
6481+__u32
6482+gr_acl_handle_rmdir(const struct dentry * dentry, const struct vfsmount * mnt)
6483+{
6484+ generic_fs_handler(dentry, mnt, GR_WRITE | GR_DELETE , GR_RMDIR_ACL_MSG);
6485+}
6486+
6487+__u32
6488+gr_acl_handle_unlink(const struct dentry *dentry, const struct vfsmount *mnt)
6489+{
6490+ generic_fs_handler(dentry, mnt, GR_WRITE | GR_DELETE , GR_UNLINK_ACL_MSG);
6491+}
6492+
6493+__u32
6494+gr_acl_handle_truncate(const struct dentry *dentry, const struct vfsmount *mnt)
6495+{
6496+ generic_fs_handler(dentry, mnt, GR_WRITE, GR_TRUNCATE_ACL_MSG);
6497+}
6498+
6499+__u32
6500+gr_acl_handle_utime(const struct dentry *dentry, const struct vfsmount *mnt)
6501+{
6502+ generic_fs_handler(dentry, mnt, GR_WRITE, GR_ATIME_ACL_MSG);
6503+}
6504+
6505+__u32
6506+gr_acl_handle_fchmod(const struct dentry *dentry, const struct vfsmount *mnt,
6507+ mode_t mode)
6508+{
6509+ if (unlikely((mode != (mode_t)-1) && (mode & (S_ISUID | S_ISGID)))) {
6510+ generic_fs_handler(dentry, mnt, GR_WRITE | GR_SETID,
6511+ GR_FCHMOD_ACL_MSG);
6512+ } else {
6513+ generic_fs_handler(dentry, mnt, GR_WRITE, GR_FCHMOD_ACL_MSG);
6514+ }
6515+}
6516+
6517+__u32
6518+gr_acl_handle_chmod(const struct dentry *dentry, const struct vfsmount *mnt,
6519+ mode_t mode)
6520+{
6521+ if (unlikely((mode != (mode_t)-1) && (mode & (S_ISUID | S_ISGID)))) {
6522+ generic_fs_handler(dentry, mnt, GR_WRITE | GR_SETID,
6523+ GR_CHMOD_ACL_MSG);
6524+ } else {
6525+ generic_fs_handler(dentry, mnt, GR_WRITE, GR_CHMOD_ACL_MSG);
6526+ }
6527+}
6528+
6529+__u32
6530+gr_acl_handle_chown(const struct dentry *dentry, const struct vfsmount *mnt)
6531+{
6532+ generic_fs_handler(dentry, mnt, GR_WRITE, GR_CHOWN_ACL_MSG);
6533+}
6534+
6535+__u32
6536+gr_acl_handle_execve(const struct dentry *dentry, const struct vfsmount *mnt)
6537+{
6538+ generic_fs_handler(dentry, mnt, GR_EXEC, GR_EXEC_ACL_MSG);
6539+}
6540+
6541+__u32
6542+gr_acl_handle_unix(const struct dentry *dentry, const struct vfsmount *mnt)
6543+{
6544+ generic_fs_handler(dentry, mnt, GR_READ | GR_WRITE,
6545+ GR_UNIXCONNECT_ACL_MSG);
6546+}
6547+
6548+__u32
6549+gr_acl_handle_filldir(const struct dentry *dentry, const struct vfsmount *mnt,
6550+ const ino_t ino)
6551+{
6552+ if (likely((unsigned long)(dentry->d_inode))) {
6553+ struct dentry d = *dentry;
6554+ struct inode inode = *(dentry->d_inode);
6555+
6556+ inode.i_ino = ino;
6557+ d.d_inode = &inode;
6558+
6559+ if (unlikely(!gr_search_file(&d, GR_FIND | GR_NOLEARN, mnt)))
6560+ return 0;
6561+ }
6562+
6563+ return 1;
6564+}
6565+
6566+__u32
6567+gr_acl_handle_link(const struct dentry * new_dentry,
6568+ const struct dentry * parent_dentry,
6569+ const struct vfsmount * parent_mnt,
6570+ const struct dentry * old_dentry,
6571+ const struct vfsmount * old_mnt, const char *to)
6572+{
6573+ __u32 needmode = GR_WRITE | GR_CREATE;
6574+ __u32 mode;
6575+
6576+ mode =
6577+ gr_check_link(new_dentry, parent_dentry, parent_mnt, old_dentry,
6578+ old_mnt);
6579+
6580+ if (unlikely(((mode & needmode) == needmode) && mode & GR_AUDITS)) {
6581+ security_audit(GR_LINK_ACL_MSG, "successful",
6582+ gr_to_filename(old_dentry, old_mnt), to,
6583+ DEFAULTSECARGS);
6584+ return mode;
6585+ } else if (unlikely(((mode & needmode) != needmode) && !(mode & GR_SUPPRESS))) {
6586+ security_alert(GR_LINK_ACL_MSG, "denied",
6587+ gr_to_filename(old_dentry, old_mnt), to,
6588+ DEFAULTSECARGS);
6589+ return 0;
6590+ } else if (unlikely((mode & needmode) != needmode))
6591+ return 0;
6592+
6593+ return (GR_WRITE | GR_CREATE);
6594+}
6595+
6596+__u32
6597+gr_acl_handle_symlink(const struct dentry * new_dentry,
6598+ const struct dentry * parent_dentry,
6599+ const struct vfsmount * parent_mnt, const char *from)
6600+{
6601+ __u32 needmode = GR_WRITE | GR_CREATE;
6602+ __u32 mode;
6603+
6604+ mode =
6605+ gr_check_create(new_dentry, parent_dentry, parent_mnt,
6606+ GR_CREATE | GR_AUDIT_CREATE |
6607+ GR_WRITE | GR_AUDIT_WRITE | GR_SUPPRESS);
6608+
6609+ if (unlikely(mode & GR_WRITE && mode & GR_AUDITS)) {
6610+ security_audit(GR_SYMLINK_ACL_MSG, "successful",
6611+ from, gr_to_filename(new_dentry, parent_mnt),
6612+ DEFAULTSECARGS);
6613+ return mode;
6614+ } else if (unlikely(((mode & needmode) != needmode) && !(mode & GR_SUPPRESS))) {
6615+ security_alert(GR_SYMLINK_ACL_MSG, "denied",
6616+ from, gr_to_filename(new_dentry, parent_mnt),
6617+ DEFAULTSECARGS);
6618+ return 0;
6619+ } else if (unlikely((mode & needmode) != needmode))
6620+ return 0;
6621+
6622+ return (GR_WRITE | GR_CREATE);
6623+}
6624+
6625+#define generic_fs_create_handler(new_dentry, parent_dentry, parent_mnt, reqmode, fmt) \
6626+{ \
6627+ __u32 mode; \
6628+ \
6629+ mode = gr_check_create(new_dentry, parent_dentry, parent_mnt, reqmode | to_gr_audit(reqmode) | GR_SUPPRESS); \
6630+ \
6631+ if (unlikely(((mode & (reqmode)) == (reqmode)) && mode & GR_AUDITS)) { \
6632+ security_audit(fmt, "successful", \
6633+ gr_to_filename(new_dentry, parent_mnt), \
6634+ DEFAULTSECARGS); \
6635+ return mode; \
6636+ } else if (unlikely((mode & (reqmode)) != (reqmode) && !(mode & GR_SUPPRESS))) { \
6637+ security_alert(fmt, "denied", \
6638+ gr_to_filename(new_dentry, parent_mnt), \
6639+ DEFAULTSECARGS); \
6640+ return 0; \
6641+ } else if (unlikely((mode & (reqmode)) != (reqmode))) \
6642+ return 0; \
6643+ \
6644+ return (reqmode); \
6645+}
6646+
6647+__u32
6648+gr_acl_handle_mknod(const struct dentry * new_dentry,
6649+ const struct dentry * parent_dentry,
6650+ const struct vfsmount * parent_mnt,
6651+ const int mode)
6652+{
6653+ __u32 reqmode = GR_WRITE | GR_CREATE;
6654+ if (unlikely(mode & (S_ISUID | S_ISGID)))
6655+ reqmode |= GR_SETID;
6656+
6657+ generic_fs_create_handler(new_dentry, parent_dentry, parent_mnt,
6658+ reqmode, GR_MKNOD_ACL_MSG);
6659+}
6660+
6661+__u32
6662+gr_acl_handle_mkdir(const struct dentry *new_dentry,
6663+ const struct dentry *parent_dentry,
6664+ const struct vfsmount *parent_mnt)
6665+{
6666+ generic_fs_create_handler(new_dentry, parent_dentry, parent_mnt,
6667+ GR_WRITE | GR_CREATE, GR_MKDIR_ACL_MSG);
6668+}
6669+
6670+#define RENAME_CHECK_SUCCESS(old, new) \
6671+ (((old & (GR_WRITE | GR_READ)) == (GR_WRITE | GR_READ)) && \
6672+ ((new & (GR_WRITE | GR_READ)) == (GR_WRITE | GR_READ)))
6673+
6674+int
6675+gr_acl_handle_rename(struct dentry *new_dentry,
6676+ struct dentry *parent_dentry,
6677+ const struct vfsmount *parent_mnt,
6678+ struct dentry *old_dentry,
6679+ struct inode *old_parent_inode,
6680+ struct vfsmount *old_mnt, const char *newname)
6681+{
6682+ __u8 gr_replace = 1;
6683+ __u32 comp1, comp2;
6684+ int error = 0;
6685+
6686+ if (unlikely(!gr_acl_is_enabled()))
6687+ return 1;
6688+
6689+ if (!new_dentry->d_inode) {
6690+ gr_replace = 0;
6691+
6692+ comp1 = gr_check_create(new_dentry, parent_dentry, parent_mnt,
6693+ GR_READ | GR_WRITE | GR_CREATE | GR_AUDIT_READ |
6694+ GR_AUDIT_WRITE | GR_AUDIT_CREATE | GR_SUPPRESS);
6695+ comp2 = gr_search_file(old_dentry, GR_READ | GR_WRITE |
6696+ GR_DELETE | GR_AUDIT_DELETE |
6697+ GR_AUDIT_READ | GR_AUDIT_WRITE |
6698+ GR_SUPPRESS, old_mnt);
6699+ } else {
6700+ comp1 = gr_search_file(new_dentry, GR_READ | GR_WRITE |
6701+ GR_CREATE | GR_DELETE |
6702+ GR_AUDIT_CREATE | GR_AUDIT_DELETE |
6703+ GR_AUDIT_READ | GR_AUDIT_WRITE |
6704+ GR_SUPPRESS, parent_mnt);
6705+ comp2 =
6706+ gr_search_file(old_dentry,
6707+ GR_READ | GR_WRITE | GR_AUDIT_READ |
6708+ GR_DELETE | GR_AUDIT_DELETE |
6709+ GR_AUDIT_WRITE | GR_SUPPRESS, old_mnt);
6710+ }
6711+
6712+ if (RENAME_CHECK_SUCCESS(comp1, comp2) &&
6713+ ((comp1 & GR_AUDITS) || (comp2 & GR_AUDITS)))
6714+ security_audit(GR_RENAME_ACL_MSG, "successful",
6715+ gr_to_filename(old_dentry, old_mnt),
6716+ newname, DEFAULTSECARGS);
6717+ else if (!RENAME_CHECK_SUCCESS(comp1, comp2) && !(comp1 & GR_SUPPRESS)
6718+ && !(comp2 & GR_SUPPRESS)) {
6719+ security_alert(GR_RENAME_ACL_MSG, "denied",
6720+ gr_to_filename(old_dentry, old_mnt), newname,
6721+ DEFAULTSECARGS);
6722+ error = -EACCES;
6723+ } else if (unlikely(!RENAME_CHECK_SUCCESS(comp1, comp2)))
6724+ error = -EACCES;
6725+
6726+ if (error)
6727+ return error;
6728+
6729+ error = gr_handle_rename(old_parent_inode, parent_dentry->d_inode,
6730+ old_dentry, new_dentry, old_mnt, gr_replace);
6731+
6732+ return error;
6733+}
6734+
6735+void
6736+gr_acl_handle_exit(void)
6737+{
6738+ u16 id;
6739+ char *rolename;
6740+
6741+ if (unlikely(current->acl_sp_role && gr_acl_is_enabled())) {
6742+ id = current->acl_role_id;
6743+ rolename = current->role->rolename;
6744+ gr_set_acls(1);
6745+ security_alert_good(GR_SPROLEL_ACL_MSG,
6746+ rolename, id, DEFAULTSECARGS);
6747+ }
6748+
6749+ if (current->exec_file) {
6750+ fput(current->exec_file);
6751+ current->exec_file = NULL;
6752+ }
6753+}
6754+
6755+int
6756+gr_acl_handle_procpidmem(const struct task_struct *task)
6757+{
6758+ if (unlikely(!gr_acl_is_enabled()))
6759+ return 0;
6760+
6761+ if (task->acl->mode & GR_PROTPROCFD)
6762+ return -EACCES;
6763+
6764+ return 0;
6765+}
6766diff -urN linux-2.4.21/grsecurity/gracl_ip.c linux-2.4.21/grsecurity/gracl_ip.c
6767--- linux-2.4.21/grsecurity/gracl_ip.c 1969-12-31 19:00:00.000000000 -0500
6768+++ linux-2.4.21/grsecurity/gracl_ip.c 2003-06-23 11:49:16.000000000 -0400
6769@@ -0,0 +1,180 @@
6770+/*
6771+ * grsecurity/gracl_ip.c
6772+ * Copyright Brad Spengler 2002, 2003
6773+ *
6774+ */
6775+
6776+#include <linux/kernel.h>
6777+#include <asm/uaccess.h>
6778+#include <asm/errno.h>
6779+#include <net/sock.h>
6780+#include <linux/file.h>
6781+#include <linux/fs.h>
6782+#include <linux/net.h>
6783+#include <linux/in.h>
6784+#include <linux/skbuff.h>
6785+#include <linux/ip.h>
6786+#include <linux/udp.h>
6787+#include <linux/smp_lock.h>
6788+#include <linux/types.h>
6789+#include <linux/sched.h>
6790+#include <linux/gracl.h>
6791+#include <linux/grsecurity.h>
6792+#include <linux/grinternal.h>
6793+
6794+#define GR_BIND 0x01
6795+#define GR_CONNECT 0x02
6796+
6797+int
6798+gr_search_socket(const int domain, const int type, const int protocol)
6799+{
6800+ struct acl_subject_label *curr;
6801+
6802+ if (unlikely(!gr_acl_is_enabled()))
6803+ goto exit;
6804+
6805+ if ((domain < 0) || (type < 0) || (protocol < 0) || (domain != PF_INET)
6806+ || (domain >= NPROTO) || (type >= SOCK_MAX) || (protocol > 255))
6807+ goto exit; // let the kernel handle it
6808+
6809+ curr = current->acl;
6810+
6811+ if (!curr->ips)
6812+ goto exit;
6813+
6814+ if ((curr->ip_type & (1 << type)) &&
6815+ (curr->ip_proto[protocol / 32] & (1 << (protocol % 32))))
6816+ goto exit;
6817+
6818+ if (curr->mode & GR_LEARN) {
6819+ /* we don't place acls on raw sockets , and sometimes
6820+ dgram/ip sockets are opened for ioctl and not
6821+ bind/connect, so we'll fake a bind learn log */
6822+ if (type == SOCK_RAW || type == SOCK_PACKET) {
6823+ __u32 fakeip = 0;
6824+ security_learn(GR_IP_LEARN_MSG, current->role->rolename,
6825+ current->role->roletype, current->uid,
6826+ current->gid, current->exec_file ?
6827+ gr_to_filename(current->exec_file->f_dentry,
6828+ current->exec_file->f_vfsmnt) :
6829+ curr->filename, curr->filename,
6830+ NIPQUAD(fakeip), 0, type,
6831+ protocol, GR_CONNECT, NIPQUAD(current->curr_ip));
6832+ } else if ((type == SOCK_DGRAM) && (protocol == IPPROTO_IP)) {
6833+ __u32 fakeip = 0;
6834+ security_learn(GR_IP_LEARN_MSG, current->role->rolename,
6835+ current->role->roletype, current->uid,
6836+ current->gid, current->exec_file ?
6837+ gr_to_filename(current->exec_file->f_dentry,
6838+ current->exec_file->f_vfsmnt) :
6839+ curr->filename, curr->filename,
6840+ NIPQUAD(fakeip), 0, type,
6841+ protocol, GR_BIND, NIPQUAD(current->curr_ip));
6842+ }
6843+ /* we'll log when they use connect or bind */
6844+ goto exit;
6845+ }
6846+
6847+ security_alert(GR_SOCK_MSG, domain, type, protocol, DEFAULTSECARGS);
6848+
6849+ return 0;
6850+ exit:
6851+ return 1;
6852+}
6853+
6854+static __inline__ int
6855+gr_search_connectbind(const int mode, const struct sock *sk,
6856+ const struct sockaddr_in *addr, const int type)
6857+{
6858+ struct acl_subject_label *curr;
6859+ struct acl_ip_label *ip;
6860+ unsigned long i;
6861+ unsigned long ip_addr = 0;
6862+ __u16 ip_port = 0;
6863+
6864+ if (unlikely(!gr_acl_is_enabled() || sk->family != PF_INET))
6865+ return 1;
6866+
6867+ curr = current->acl;
6868+
6869+ if (!curr->ips)
6870+ return 1;
6871+
6872+ ip_addr = addr->sin_addr.s_addr;
6873+ ip_port = ntohs(addr->sin_port);
6874+
6875+ for (i = 0; i < curr->ip_num; i++) {
6876+ ip = *(curr->ips + i);
6877+ if ((ip->mode & mode) &&
6878+ (ip_port >= ip->low) &&
6879+ (ip_port <= ip->high) &&
6880+ ((ntohl(ip_addr) & ip->netmask) ==
6881+ (ntohl(ip->addr) & ip->netmask))
6882+ && (ip->
6883+ proto[sk->protocol / 32] & (1 << (sk->protocol % 32)))
6884+ && (ip->type & (1 << type)))
6885+ return 1;
6886+ }
6887+
6888+ if (curr->mode & GR_LEARN) {
6889+ security_learn(GR_IP_LEARN_MSG, current->role->rolename,
6890+ current->role->roletype, current->uid,
6891+ current->gid, current->exec_file ?
6892+ gr_to_filename(current->exec_file->f_dentry,
6893+ current->exec_file->f_vfsmnt) :
6894+ curr->filename, curr->filename,
6895+ NIPQUAD(ip_addr), ip_port, type,
6896+ sk->protocol, mode, NIPQUAD(current->curr_ip));
6897+ return 1;
6898+ }
6899+
6900+ if (mode == GR_BIND)
6901+ security_alert(GR_BIND_ACL_MSG, NIPQUAD(ip_addr), ip_port,
6902+ type, sk->protocol, DEFAULTSECARGS);
6903+ else if (mode == GR_CONNECT)
6904+ security_alert(GR_CONNECT_ACL_MSG, NIPQUAD(ip_addr), ip_port,
6905+ type, sk->protocol, DEFAULTSECARGS);
6906+
6907+ return 0;
6908+}
6909+
6910+int
6911+gr_search_connect(const struct socket *sock, const struct sockaddr_in *addr)
6912+{
6913+ return gr_search_connectbind(GR_CONNECT, sock->sk, addr, sock->type);
6914+}
6915+
6916+int
6917+gr_search_bind(const struct socket *sock, const struct sockaddr_in *addr)
6918+{
6919+ return gr_search_connectbind(GR_BIND, sock->sk, addr, sock->type);
6920+}
6921+
6922+int
6923+gr_search_udp_sendmsg(const struct sock *sk, const struct sockaddr_in *addr)
6924+{
6925+ if (addr)
6926+ return gr_search_connectbind(GR_CONNECT, sk, addr, SOCK_DGRAM);
6927+ else {
6928+ struct sockaddr_in sin;
6929+
6930+ sin.sin_addr.s_addr = sk->daddr;
6931+ sin.sin_port = sk->dport;
6932+
6933+ return gr_search_connectbind(GR_CONNECT, sk, &sin, SOCK_DGRAM);
6934+ }
6935+}
6936+
6937+int
6938+gr_search_udp_recvmsg(const struct sock *sk, const struct sk_buff *skb)
6939+{
6940+ struct sockaddr_in sin;
6941+
6942+ if (unlikely(skb->len < sizeof (struct udphdr)))
6943+ return 1; // skip this packet
6944+
6945+ sin.sin_addr.s_addr = skb->nh.iph->saddr;
6946+ sin.sin_port = skb->h.uh->source;
6947+
6948+ return gr_search_connectbind(GR_CONNECT, sk, &sin, SOCK_DGRAM);
6949+}
6950diff -urN linux-2.4.21/grsecurity/gracl_learn.c linux-2.4.21/grsecurity/gracl_learn.c
6951--- linux-2.4.21/grsecurity/gracl_learn.c 1969-12-31 19:00:00.000000000 -0500
6952+++ linux-2.4.21/grsecurity/gracl_learn.c 2003-06-23 11:49:16.000000000 -0400
6953@@ -0,0 +1,120 @@
6954+#include <linux/kernel.h>
6955+#include <linux/mm.h>
6956+#include <linux/sched.h>
6957+#include <linux/poll.h>
6958+#include <linux/string.h>
6959+#include <linux/file.h>
6960+#include <linux/types.h>
6961+#include <linux/vmalloc.h>
6962+#include <linux/grinternal.h>
6963+
6964+extern ssize_t write_grsec_handler(struct file * file, const char * buf,
6965+ size_t count, loff_t *ppos);
6966+
6967+static DECLARE_WAIT_QUEUE_HEAD(learn_wait);
6968+
6969+char *learn_buffer[80][NR_CPUS];
6970+ssize_t learn_buffer_len[80][NR_CPUS];
6971+
6972+static ssize_t
6973+read_learn(struct file *file, char * buf, size_t count, loff_t * ppos)
6974+{
6975+ DECLARE_WAITQUEUE(wait, current);
6976+ ssize_t retval = 0, tmp;
6977+ int i, j;
6978+
6979+ add_wait_queue(&learn_wait, &wait);
6980+ set_current_state(TASK_INTERRUPTIBLE);
6981+ do {
6982+ for (j = 0; j < 80; j++)
6983+ for (i = 0; i < NR_CPUS; i++)
6984+ if (learn_buffer_len[j][i])
6985+ goto new_data;
6986+ if (file->f_flags & O_NONBLOCK) {
6987+ retval = -EAGAIN;
6988+ goto out;
6989+ }
6990+ if (signal_pending(current)) {
6991+ retval = -ERESTARTSYS;
6992+ goto out;
6993+ }
6994+
6995+ schedule();
6996+ } while (1);
6997+
6998+new_data:
6999+ for (j = 0; j < 80; j++) {
7000+ for (i = 0; i < NR_CPUS; i++) {
7001+ tmp = learn_buffer_len[j][i];
7002+ if (!tmp)
7003+ continue;
7004+ if (count < ((i * 80 * 16384) + (j * 16384) + tmp)) {
7005+ retval = -EINVAL;
7006+ goto out;
7007+ }
7008+ if (copy_to_user(buf + (i * 80 * 16384) + (j * 16384), learn_buffer[j][i], tmp)) {
7009+ retval = -EFAULT;
7010+ goto out;
7011+ } else {
7012+ retval += tmp;
7013+ learn_buffer_len[j][i] = 0;
7014+ }
7015+ }
7016+ }
7017+
7018+out:
7019+ set_current_state(TASK_RUNNING);
7020+ remove_wait_queue(&learn_wait, &wait);
7021+ return retval;
7022+}
7023+
7024+static unsigned int
7025+poll_learn(struct file * file, poll_table * wait)
7026+{
7027+ int i, j;
7028+
7029+ poll_wait(file, &learn_wait, wait);
7030+
7031+ for(j = 0; j < 80; j++)
7032+ for (i = 0; i < NR_CPUS; i++)
7033+ if (learn_buffer_len[j][i])
7034+ return (POLLIN | POLLRDNORM);
7035+
7036+ return 0;
7037+}
7038+
7039+void
7040+gr_add_learn_entry(const char *fmt, ...)
7041+{
7042+ va_list args;
7043+ int j;
7044+ int cpu = smp_processor_id();
7045+
7046+ for(j = 0; j < 80; j++) {
7047+ if (learn_buffer_len[j][cpu])
7048+ continue;
7049+
7050+ if (!learn_buffer[j][cpu]) {
7051+ learn_buffer[j][cpu] = vmalloc(16384);
7052+ if (!learn_buffer[j][cpu])
7053+ return;
7054+ }
7055+
7056+ va_start(args, fmt);
7057+ learn_buffer_len[j][cpu] = vsnprintf(learn_buffer[j][cpu], 16384, fmt, args);
7058+ va_end(args);
7059+
7060+ if (learn_buffer_len[j][cpu] < 0)
7061+ learn_buffer_len[j][cpu] = 16384;
7062+ wake_up_interruptible(&learn_wait);
7063+ break;
7064+ }
7065+
7066+ return;
7067+}
7068+
7069+struct file_operations grsec_fops = {
7070+ read: read_learn,
7071+ write: write_grsec_handler,
7072+ poll: poll_learn,
7073+};
7074diff -urN linux-2.4.21/grsecurity/gracl_res.c linux-2.4.21/grsecurity/gracl_res.c
7075--- linux-2.4.21/grsecurity/gracl_res.c 1969-12-31 19:00:00.000000000 -0500
7076+++ linux-2.4.21/grsecurity/gracl_res.c 2003-06-23 11:49:16.000000000 -0400
7077@@ -0,0 +1,42 @@
7078+/* resource handling routines (c) Brad Spengler 2002, 2003 */
7079+
7080+#include <linux/kernel.h>
7081+#include <linux/sched.h>
7082+#include <linux/gracl.h>
7083+#include <linux/grinternal.h>
7084+
7085+static const char *restab_log[11] = {
7086+ "RLIMIT_CPU",
7087+ "RLIMIT_FSIZE",
7088+ "RLIMIT_DATA",
7089+ "RLIMIT_STACK",
7090+ "RLIMIT_CORE",
7091+ "RLIMIT_RSS",
7092+ "RLIMIT_NPROC",
7093+ "RLIMIT_NOFILE",
7094+ "RLIMIT_MEMLOCK",
7095+ "RLIMIT_AS",
7096+ "RLIMIT_LOCKS"
7097+};
7098+
7099+__inline__ void
7100+gr_log_resource(const struct task_struct *task,
7101+ const int res, const unsigned long wanted)
7102+{
7103+ if (unlikely(res == RLIMIT_NPROC &&
7104+ (cap_raised(task->cap_effective, CAP_SYS_ADMIN) ||
7105+ cap_raised(task->cap_effective, CAP_SYS_RESOURCE))))
7106+ return;
7107+
7108+ if (unlikely(wanted >= task->rlim[res].rlim_cur &&
7109+ task->rlim[res].rlim_cur != RLIM_INFINITY))
7110+ security_alert(GR_RESOURCE_MSG, wanted, restab_log[res],
7111+ task->rlim[res].rlim_cur, task->comm,
7112+ task->pid, task->uid, task->euid,
7113+ task->gid, task->egid, task->p_pptr->comm,
7114+ task->p_pptr->pid, task->p_pptr->uid,
7115+ task->p_pptr->euid, task->p_pptr->gid,
7116+ task->p_pptr->egid);
7117+
7118+ return;
7119+}
7120diff -urN linux-2.4.21/grsecurity/gracl_segv.c linux-2.4.21/grsecurity/gracl_segv.c
7121--- linux-2.4.21/grsecurity/gracl_segv.c 1969-12-31 19:00:00.000000000 -0500
7122+++ linux-2.4.21/grsecurity/gracl_segv.c 2003-06-23 11:49:16.000000000 -0400
7123@@ -0,0 +1,323 @@
7124+/*
7125+ * grsecurity/gracl_segv.c
7126+ * Copyright Brad Spengler 2002, 2003
7127+ *
7128+ */
7129+
7130+#include <linux/kernel.h>
7131+#include <linux/mm.h>
7132+#include <asm/uaccess.h>
7133+#include <asm/errno.h>
7134+#include <asm/mman.h>
7135+#include <net/sock.h>
7136+#include <linux/file.h>
7137+#include <linux/fs.h>
7138+#include <linux/net.h>
7139+#include <linux/in.h>
7140+#include <linux/smp_lock.h>
7141+#include <linux/slab.h>
7142+#include <linux/types.h>
7143+#include <linux/sched.h>
7144+#include <linux/timer.h>
7145+#include <linux/gracl.h>
7146+#include <linux/grsecurity.h>
7147+#include <linux/grinternal.h>
7148+
7149+static struct crash_uid *uid_set;
7150+static unsigned short uid_used;
7151+static rwlock_t gr_uid_lock = RW_LOCK_UNLOCKED;
7152+extern rwlock_t gr_inode_lock;
7153+extern __inline__ struct acl_subject_label *lookup_acl_subj_label(const ino_t
7154+ inode,
7155+ const kdev_t
7156+ dev,
7157+ struct
7158+ acl_role_label
7159+ *role);
7160+
7161+int
7162+gr_init_uidset(void)
7163+{
7164+ uid_set =
7165+ kmalloc(GR_UIDTABLE_MAX * sizeof (struct crash_uid), GFP_KERNEL);
7166+ uid_used = 0;
7167+
7168+ return uid_set ? 1 : 0;
7169+}
7170+
7171+void
7172+gr_free_uidset(void)
7173+{
7174+ if (uid_set)
7175+ kfree(uid_set);
7176+
7177+ return;
7178+}
7179+
7180+int
7181+gr_find_uid(const uid_t uid)
7182+{
7183+ struct crash_uid *tmp = uid_set;
7184+ uid_t buid;
7185+ int low = 0, high = uid_used - 1, mid;
7186+
7187+ while (high >= low) {
7188+ mid = (low + high) >> 1;
7189+ buid = tmp[mid].uid;
7190+ if (buid == uid)
7191+ return mid;
7192+ if (buid > uid)
7193+ high = mid - 1;
7194+ if (buid < uid)
7195+ low = mid + 1;
7196+ }
7197+
7198+ return -1;
7199+}
7200+
7201+static __inline__ void
7202+gr_insertsort(void)
7203+{
7204+ unsigned short i, j;
7205+ struct crash_uid index;
7206+
7207+ for (i = 1; i < uid_used; i++) {
7208+ index = uid_set[i];
7209+ j = i;
7210+ while ((j > 0) && uid_set[j - 1].uid > index.uid) {
7211+ uid_set[j] = uid_set[j - 1];
7212+ j--;
7213+ }
7214+ uid_set[j] = index;
7215+ }
7216+
7217+ return;
7218+}
7219+
7220+static __inline__ void
7221+gr_insert_uid(const uid_t uid, const unsigned long expires)
7222+{
7223+ int loc;
7224+
7225+ if (uid_used == GR_UIDTABLE_MAX)
7226+ return;
7227+
7228+ loc = gr_find_uid(uid);
7229+
7230+ if (loc >= 0) {
7231+ uid_set[loc].expires = expires;
7232+ return;
7233+ }
7234+
7235+ uid_set[uid_used].uid = uid;
7236+ uid_set[uid_used].expires = expires;
7237+ uid_used++;
7238+
7239+ gr_insertsort();
7240+
7241+ return;
7242+}
7243+
7244+void
7245+gr_remove_uid(const unsigned short loc)
7246+{
7247+ unsigned short i;
7248+
7249+ for (i = loc + 1; i < uid_used; i++)
7250+ uid_set[i - i] = uid_set[i];
7251+
7252+ uid_used--;
7253+
7254+ return;
7255+}
7256+
7257+int
7258+gr_check_crash_uid(const uid_t uid)
7259+{
7260+ int loc;
7261+
7262+ if (unlikely(!gr_acl_is_enabled()))
7263+ return 0;
7264+
7265+ read_lock(&gr_uid_lock);
7266+ loc = gr_find_uid(uid);
7267+ read_unlock(&gr_uid_lock);
7268+
7269+ if (loc < 0)
7270+ return 0;
7271+
7272+ write_lock(&gr_uid_lock);
7273+ if (time_before_eq(uid_set[loc].expires, jiffies))
7274+ gr_remove_uid(loc);
7275+ else {
7276+ write_unlock(&gr_uid_lock);
7277+ return 1;
7278+ }
7279+
7280+ write_unlock(&gr_uid_lock);
7281+ return 0;
7282+}
7283+
7284+static __inline__ int
7285+proc_is_setxid(const struct task_struct *task)
7286+{
7287+ if (task->uid != task->euid || task->uid != task->suid ||
7288+ task->uid != task->fsuid)
7289+ return 1;
7290+ if (task->gid != task->egid || task->gid != task->sgid ||
7291+ task->gid != task->fsgid)
7292+ return 1;
7293+
7294+ return 0;
7295+}
7296+static __inline__ int
7297+gr_fake_force_sig(int sig, struct task_struct *t)
7298+{
7299+ unsigned long int flags;
7300+
7301+ spin_lock_irqsave(&t->sigmask_lock, flags);
7302+ if (t->sig == NULL) {
7303+ spin_unlock_irqrestore(&t->sigmask_lock, flags);
7304+ return -ESRCH;
7305+ }
7306+
7307+ if (t->sig->action[sig - 1].sa.sa_handler == SIG_IGN)
7308+ t->sig->action[sig - 1].sa.sa_handler = SIG_DFL;
7309+ sigdelset(&t->blocked, sig);
7310+ recalc_sigpending(t);
7311+ spin_unlock_irqrestore(&t->sigmask_lock, flags);
7312+
7313+ return send_sig_info(sig, (void *) 1L, t);
7314+}
7315+
7316+void
7317+gr_handle_crash(struct task_struct *task, const int sig)
7318+{
7319+ struct acl_subject_label *curr;
7320+ struct acl_subject_label *curr2;
7321+ struct task_struct *tsk;
7322+
7323+ if (sig != SIGSEGV && sig != SIGKILL && sig != SIGBUS && sig != SIGILL)
7324+ return;
7325+
7326+ if (unlikely(!gr_acl_is_enabled()))
7327+ return;
7328+
7329+ curr = task->acl;
7330+
7331+ if (!(curr->resmask & (1 << GR_CRASH_RES)))
7332+ return;
7333+
7334+ if (time_before_eq(curr->expires, jiffies)) {
7335+ curr->expires = 0;
7336+ curr->crashes = 0;
7337+ }
7338+
7339+ curr->crashes++;
7340+
7341+ if (!curr->expires)
7342+ curr->expires = jiffies + curr->res[GR_CRASH_RES].rlim_max;
7343+
7344+ if ((curr->crashes >= curr->res[GR_CRASH_RES].rlim_cur) &&
7345+ time_after(curr->expires, jiffies)) {
7346+ if (task->uid && proc_is_setxid(task)) {
7347+ security_alert(GR_SEGVSTART_ACL_MSG, task->comm,
7348+ task->pid, task->uid, task->euid,
7349+ task->gid, task->egid,
7350+ task->p_pptr->comm, task->p_pptr->pid,
7351+ task->p_pptr->uid, task->p_pptr->euid,
7352+ task->p_pptr->gid, task->p_pptr->egid,
7353+ task->uid,
7354+ curr->res[GR_CRASH_RES].rlim_max / HZ);
7355+ write_lock(&gr_uid_lock);
7356+ gr_insert_uid(task->uid, curr->expires);
7357+ write_unlock(&gr_uid_lock);
7358+ curr->expires = 0;
7359+ curr->crashes = 0;
7360+ read_lock(&tasklist_lock);
7361+ for_each_task(tsk) {
7362+ if (tsk != task && tsk->uid == task->uid)
7363+ gr_fake_force_sig(SIGKILL, tsk);
7364+ }
7365+ read_unlock(&tasklist_lock);
7366+ } else {
7367+ security_alert(GR_SEGVNOSUID_ACL_MSG, task->comm,
7368+ task->pid, task->uid, task->euid,
7369+ task->gid, task->egid,
7370+ task->p_pptr->comm, task->p_pptr->pid,
7371+ task->p_pptr->uid, task->p_pptr->euid,
7372+ task->p_pptr->gid, task->p_pptr->egid,
7373+ kdevname(curr->device), curr->inode,
7374+ curr->res[GR_CRASH_RES].rlim_max / HZ);
7375+ read_lock(&tasklist_lock);
7376+ for_each_task(tsk) {
7377+ if (likely(tsk != task)) {
7378+ curr2 = tsk->acl;
7379+
7380+ if (curr2->device == curr->device &&
7381+ curr2->inode == curr->inode)
7382+ gr_fake_force_sig(SIGKILL, tsk);
7383+ }
7384+ }
7385+ read_unlock(&tasklist_lock);
7386+ }
7387+ }
7388+
7389+ return;
7390+}
7391+
7392+int
7393+gr_check_crash_exec(const struct file *filp)
7394+{
7395+ struct acl_subject_label *curr;
7396+
7397+ if (unlikely(!gr_acl_is_enabled()))
7398+ return 0;
7399+
7400+ read_lock(&gr_inode_lock);
7401+ curr = lookup_acl_subj_label(filp->f_dentry->d_inode->i_ino,
7402+ filp->f_dentry->d_inode->i_dev,
7403+ current->role);
7404+ read_unlock(&gr_inode_lock);
7405+
7406+ if (!curr || !(curr->resmask & (1 << GR_CRASH_RES)) ||
7407+ (!curr->crashes && !curr->expires))
7408+ return 0;
7409+
7410+ if ((curr->crashes >= curr->res[GR_CRASH_RES].rlim_cur) &&
7411+ time_after(curr->expires, jiffies))
7412+ return 1;
7413+ else if (time_before_eq(curr->expires, jiffies)) {
7414+ curr->crashes = 0;
7415+ curr->expires = 0;
7416+ }
7417+
7418+ return 0;
7419+}
7420+
7421+void
7422+gr_handle_alertkill(void)
7423+{
7424+ struct acl_subject_label *curracl;
7425+ __u32 curr_ip;
7426+ struct task_struct *task;
7427+
7428+ if (unlikely(!gr_acl_is_enabled()))
7429+ return;
7430+
7431+ curracl = current->acl;
7432+ curr_ip = current->curr_ip;
7433+
7434+ if ((curracl->mode & GR_KILLIPPROC) && curr_ip &&
7435+ (curr_ip != 0xffffffff)) {
7436+ read_lock(&tasklist_lock);
7437+ for_each_task(task) {
7438+ if (task->curr_ip == curr_ip)
7439+ gr_fake_force_sig(SIGKILL, task);
7440+ }
7441+ read_unlock(&tasklist_lock);
7442+ } else if (curracl->mode & GR_KILLPROC)
7443+ gr_fake_force_sig(SIGKILL, current);
7444+
7445+ return;
7446+}
7447diff -urN linux-2.4.21/grsecurity/gracl_shm.c linux-2.4.21/grsecurity/gracl_shm.c
7448--- linux-2.4.21/grsecurity/gracl_shm.c 1969-12-31 19:00:00.000000000 -0500
7449+++ linux-2.4.21/grsecurity/gracl_shm.c 2003-06-23 11:49:16.000000000 -0400
7450@@ -0,0 +1,36 @@
7451+/* shared memory handling routines, (c) Brad Spengler 2002, 2003 */
7452+
7453+#include <linux/kernel.h>
7454+#include <linux/mm.h>
7455+#include <linux/sched.h>
7456+#include <linux/file.h>
7457+#include <linux/ipc.h>
7458+#include <linux/gracl.h>
7459+#include <linux/grsecurity.h>
7460+#include <linux/grinternal.h>
7461+
7462+int
7463+gr_handle_shmat(const pid_t shm_cprid, const pid_t shm_lapid,
7464+ const time_t shm_createtime, const uid_t cuid, const int shmid)
7465+{
7466+ struct task_struct *task;
7467+
7468+ if (!gr_acl_is_enabled())
7469+ return 1;
7470+
7471+ task = find_task_by_pid(shm_cprid);
7472+
7473+ if (unlikely(!task))
7474+ task = find_task_by_pid(shm_lapid);
7475+
7476+ if (unlikely(task && ((task->start_time < shm_createtime) ||
7477+ (task->pid == shm_lapid)) &&
7478+ (task->acl->mode & GR_PROTSHM) &&
7479+ (task->acl != current->acl))) {
7480+ security_alert(GR_SHMAT_ACL_MSG, cuid, shm_cprid, shmid,
7481+ DEFAULTSECARGS);
7482+ return 0;
7483+ }
7484+
7485+ return 1;
7486+}
7487diff -urN linux-2.4.21/grsecurity/grsec_chdir.c linux-2.4.21/grsecurity/grsec_chdir.c
7488--- linux-2.4.21/grsecurity/grsec_chdir.c 1969-12-31 19:00:00.000000000 -0500
7489+++ linux-2.4.21/grsecurity/grsec_chdir.c 2003-06-23 11:49:16.000000000 -0400
7490@@ -0,0 +1,20 @@
7491+#include <linux/kernel.h>
7492+#include <linux/sched.h>
7493+#include <linux/fs.h>
7494+#include <linux/file.h>
7495+#include <linux/grsecurity.h>
7496+#include <linux/grinternal.h>
7497+
7498+void
7499+gr_log_chdir(const struct dentry *dentry, const struct vfsmount *mnt)
7500+{
7501+#ifdef CONFIG_GRKERNSEC_AUDIT_CHDIR
7502+ if ((grsec_enable_chdir && grsec_enable_group &&
7503+ in_group_p(grsec_audit_gid)) || (grsec_enable_chdir &&
7504+ !grsec_enable_group)) {
7505+ security_audit(GR_CHDIR_AUDIT_MSG, gr_to_filename(dentry, mnt),
7506+ DEFAULTSECARGS);
7507+ }
7508+#endif
7509+ return;
7510+}
7511diff -urN linux-2.4.21/grsecurity/grsec_chroot.c linux-2.4.21/grsecurity/grsec_chroot.c
7512--- linux-2.4.21/grsecurity/grsec_chroot.c 1969-12-31 19:00:00.000000000 -0500
7513+++ linux-2.4.21/grsecurity/grsec_chroot.c 2003-06-23 11:49:16.000000000 -0400
7514@@ -0,0 +1,322 @@
7515+#include <linux/kernel.h>
7516+#include <linux/sched.h>
7517+#include <linux/file.h>
7518+#include <linux/fs.h>
7519+#include <linux/types.h>
7520+#include <linux/grinternal.h>
7521+
7522+int
7523+gr_handle_chroot_unix(const pid_t pid)
7524+{
7525+#ifdef CONFIG_GRKERNSEC_CHROOT_UNIX
7526+ struct task_struct *p, **htable;
7527+
7528+ if (unlikely(!grsec_enable_chroot_unix))
7529+ return 1;
7530+
7531+ if (likely(!proc_is_chrooted(current)))
7532+ return 1;
7533+
7534+ read_lock(&tasklist_lock);
7535+
7536+ htable = &pidhash[pid_hashfn(pid)];
7537+
7538+ for (p = *htable; p && p->pid != pid; p = p->pidhash_next) ;
7539+
7540+ if (unlikely(p && !have_same_root(current, p))) {
7541+ read_unlock(&tasklist_lock);
7542+ security_alert(GR_UNIX_CHROOT_MSG, DEFAULTSECARGS);
7543+ return 0;
7544+ }
7545+ read_unlock(&tasklist_lock);
7546+#endif
7547+ return 1;
7548+}
7549+
7550+int
7551+gr_handle_chroot_nice(void)
7552+{
7553+#ifdef CONFIG_GRKERNSEC_CHROOT_NICE
7554+ if (grsec_enable_chroot_nice && proc_is_chrooted(current)) {
7555+ security_alert(GR_NICE_CHROOT_MSG, DEFAULTSECARGS);
7556+ return -EPERM;
7557+ }
7558+#endif
7559+ return 0;
7560+}
7561+
7562+int
7563+gr_handle_chroot_setpriority(task_t *p, const int niceval)
7564+{
7565+#ifdef CONFIG_GRKERNSEC_CHROOT_NICE
7566+ if (grsec_enable_chroot_nice && (!have_same_root(p, current)
7567+ || (have_same_root(p, current)
7568+ && (niceval < task_nice(p))
7569+ && proc_is_chrooted(current)))) {
7570+ security_alert(GR_PRIORITY_CHROOT_MSG, p->comm, p->pid,
7571+ DEFAULTSECARGS);
7572+ return -ESRCH;
7573+ }
7574+#endif
7575+ return 0;
7576+}
7577+
7578+int
7579+gr_handle_chroot_capset(const struct task_struct *target)
7580+{
7581+#ifdef CONFIG_GRKERNSEC_CHROOT_CAPS
7582+ if (grsec_enable_chroot_caps && proc_is_chrooted(current) &&
7583+ !have_same_root(current, target)) {
7584+ security_alert(GR_CAPSET_CHROOT_MSG, target->comm, target->pid,
7585+ DEFAULTSECARGS);
7586+ return 1;
7587+ }
7588+#endif
7589+ return 0;
7590+}
7591+
7592+int
7593+gr_pid_is_chrooted(const struct task_struct *p)
7594+{
7595+#ifdef CONFIG_GRKERNSEC_CHROOT_FINDTASK
7596+ if (!grsec_enable_chroot_findtask || (current->pid <= 1))
7597+ return 0;
7598+
7599+ if (p && p->fs && p->fs->root && p->fs->root->d_inode &&
7600+ child_reaper && child_reaper->fs && child_reaper->fs->root &&
7601+ child_reaper->fs->root->d_inode && current && current->fs &&
7602+ current->fs->root && current->fs->root->d_inode) {
7603+ if (proc_is_chrooted(current) && !have_same_root(current, p))
7604+ return 1;
7605+ }
7606+#endif
7607+ return 0;
7608+}
7609+
7610+int
7611+gr_chroot_fchdir(struct dentry *u_dentry, struct vfsmount *u_mnt)
7612+{
7613+#ifdef CONFIG_GRKERNSEC_CHROOT_FCHDIR
7614+ if (!grsec_enable_chroot_fchdir)
7615+ return 1;
7616+
7617+ if (!proc_is_chrooted(current))
7618+ return 1;
7619+ else {
7620+ struct dentry *dentry = u_dentry;
7621+ struct vfsmount *mnt = u_mnt;
7622+ struct dentry *realroot;
7623+ struct vfsmount *realrootmnt;
7624+ struct dentry *currentroot;
7625+ struct vfsmount *currentmnt;
7626+
7627+ read_lock(&child_reaper->fs->lock);
7628+ realrootmnt = mntget(child_reaper->fs->rootmnt);
7629+ realroot = dget(child_reaper->fs->root);
7630+ read_unlock(&child_reaper->fs->lock);
7631+
7632+ read_lock(&current->fs->lock);
7633+ currentmnt = mntget(current->fs->rootmnt);
7634+ currentroot = dget(current->fs->root);
7635+ read_unlock(&current->fs->lock);
7636+
7637+ spin_lock(&dcache_lock);
7638+ for (;;) {
7639+ if (unlikely
7640+ ((dentry == realroot && mnt == realrootmnt)
7641+ || (dentry == currentroot && mnt == currentmnt)))
7642+ break;
7643+ if (unlikely
7644+ (dentry == mnt->mnt_root || IS_ROOT(dentry))) {
7645+ if (mnt->mnt_parent == mnt)
7646+ break;
7647+ dentry = mnt->mnt_mountpoint;
7648+ mnt = mnt->mnt_parent;
7649+ continue;
7650+ }
7651+ dentry = dentry->d_parent;
7652+ }
7653+ spin_unlock(&dcache_lock);
7654+
7655+ dput(currentroot);
7656+ mntput(currentmnt);
7657+
7658+ if (dentry == realroot && mnt == realrootmnt) {
7659+ /* ok, they're definitely trying to fchdir outside of the
7660+ chroot. */
7661+ dput(realroot);
7662+ mntput(realrootmnt);
7663+ security_alert(GR_CHROOT_FCHDIR_MSG,
7664+ gr_to_filename(u_dentry, u_mnt),
7665+ DEFAULTSECARGS);
7666+ return 0;
7667+ } else {
7668+ dput(realroot);
7669+ mntput(realrootmnt);
7670+ return 1;
7671+ }
7672+ }
7673+#endif
7674+ return 1;
7675+}
7676+
7677+int
7678+gr_chroot_shmat(const pid_t shm_cprid, const pid_t shm_lapid,
7679+ const time_t shm_createtime)
7680+{
7681+#ifdef CONFIG_GRKERNSEC_CHROOT_SHMAT
7682+ struct task_struct *p, **htable;
7683+
7684+ if (unlikely(!grsec_enable_chroot_shmat))
7685+ return 1;
7686+
7687+ if (likely(!proc_is_chrooted(current)))
7688+ return 1;
7689+
7690+ read_lock(&tasklist_lock);
7691+
7692+ htable = &pidhash[pid_hashfn(shm_cprid)];
7693+
7694+ for (p = *htable; p && p->pid != shm_cprid; p = p->pidhash_next) ;
7695+
7696+ if (unlikely(p && !have_same_root(current, p) &&
7697+ (p->start_time < shm_createtime))) {
7698+ read_unlock(&tasklist_lock);
7699+ security_alert(GR_SHMAT_CHROOT_MSG, DEFAULTSECARGS);
7700+ return 0;
7701+ }
7702+
7703+ if (unlikely(!p)) {
7704+ htable = &pidhash[pid_hashfn(shm_lapid)];
7705+ for (p = *htable; p && p->pid != shm_lapid;
7706+ p = p->pidhash_next) ;
7707+
7708+ if (unlikely(p && !have_same_root(current, p))) {
7709+ read_unlock(&tasklist_lock);
7710+ security_alert(GR_SHMAT_CHROOT_MSG, DEFAULTSECARGS);
7711+ return 0;
7712+ }
7713+ }
7714+
7715+ read_unlock(&tasklist_lock);
7716+#endif
7717+ return 1;
7718+}
7719+
7720+void
7721+gr_log_chroot_exec(const struct dentry *dentry, const struct vfsmount *mnt)
7722+{
7723+#ifdef CONFIG_GRKERNSEC_CHROOT_EXECLOG
7724+ if (grsec_enable_chroot_execlog && proc_is_chrooted(current))
7725+ security_audit(GR_EXEC_CHROOT_MSG, gr_to_filename(dentry, mnt),
7726+ DEFAULTSECARGS);
7727+#endif
7728+ return;
7729+}
7730+
7731+int
7732+gr_handle_chroot_mknod(const struct dentry *dentry,
7733+ const struct vfsmount *mnt, const int mode)
7734+{
7735+#ifdef CONFIG_GRKERNSEC_CHROOT_MKNOD
7736+ if (grsec_enable_chroot_mknod && !S_ISFIFO(mode) &&
7737+ proc_is_chrooted(current)) {
7738+ security_alert(GR_MKNOD_CHROOT_MSG,
7739+ gr_to_filename(dentry, mnt), DEFAULTSECARGS);
7740+ return -EPERM;
7741+ }
7742+#endif
7743+ return 0;
7744+}
7745+
7746+int
7747+gr_handle_chroot_mount(const struct dentry *dentry,
7748+ const struct vfsmount *mnt, const char *dev_name)
7749+{
7750+#ifdef CONFIG_GRKERNSEC_CHROOT_MOUNT
7751+ if (grsec_enable_chroot_mount && proc_is_chrooted(current)) {
7752+ security_alert(GR_MOUNT_CHROOT_MSG, dev_name,
7753+ gr_to_filename(dentry, mnt), DEFAULTSECARGS);
7754+ return -EPERM;
7755+ }
7756+#endif
7757+ return 0;
7758+}
7759+
7760+int
7761+gr_handle_chroot_pivot(void)
7762+{
7763+#ifdef CONFIG_GRKERNSEC_CHROOT_PIVOT
7764+ if (grsec_enable_chroot_pivot && proc_is_chrooted(current)) {
7765+ security_alert(GR_PIVOT_CHROOT_MSG, DEFAULTSECARGS);
7766+ return -EPERM;
7767+ }
7768+#endif
7769+ return 0;
7770+}
7771+
7772+int
7773+gr_handle_chroot_chroot(const struct dentry *dentry, const struct vfsmount *mnt)
7774+{
7775+#ifdef CONFIG_GRKERNSEC_CHROOT_DOUBLE
7776+ if (grsec_enable_chroot_double && proc_is_chrooted(current)) {
7777+ security_alert(GR_CHROOT_CHROOT_MSG,
7778+ gr_to_filename(dentry, mnt), DEFAULTSECARGS);
7779+ return -EPERM;
7780+ }
7781+#endif
7782+ return 0;
7783+}
7784+
7785+void
7786+gr_handle_chroot_caps(struct task_struct *task)
7787+{
7788+#ifdef CONFIG_GRKERNSEC_CHROOT_CAPS
7789+ if (grsec_enable_chroot_caps && proc_is_chrooted(task)) {
7790+ task->cap_permitted =
7791+ cap_drop(task->cap_permitted, GR_CHROOT_CAPS);
7792+ task->cap_inheritable =
7793+ cap_drop(task->cap_inheritable, GR_CHROOT_CAPS);
7794+ task->cap_effective =
7795+ cap_drop(task->cap_effective, GR_CHROOT_CAPS);
7796+ }
7797+#endif
7798+ return;
7799+}
7800+
7801+int
7802+gr_handle_chroot_sysctl(const int op)
7803+{
7804+#ifdef CONFIG_GRKERNSEC_CHROOT_SYSCTL
7805+ if (grsec_enable_chroot_sysctl && proc_is_chrooted(current)
7806+ && (op & 002))
7807+ return -EACCES;
7808+#endif
7809+ return 0;
7810+}
7811+
7812+void
7813+gr_handle_chroot_chdir(struct dentry *dentry, struct vfsmount *mnt)
7814+{
7815+#ifdef CONFIG_GRKERNSEC_CHROOT_CHDIR
7816+ if (grsec_enable_chroot_chdir)
7817+ set_fs_pwd(current->fs, mnt, dentry);
7818+#endif
7819+ return;
7820+}
7821+
7822+int
7823+gr_handle_chroot_chmod(const struct dentry *dentry,
7824+ const struct vfsmount *mnt, const int mode)
7825+{
7826+#ifdef CONFIG_GRKERNSEC_CHROOT_CHMOD
7827+ if (grsec_enable_chroot_chmod &&
7828+ ((mode & S_ISUID) || (mode & S_ISGID)) &&
7829+ proc_is_chrooted(current)) {
7830+ security_alert(GR_CHMOD_CHROOT_MSG,
7831+ gr_to_filename(dentry, mnt), DEFAULTSECARGS);
7832+ return -EPERM;
7833+ }
7834+#endif
7835+ return 0;
7836+}
7837diff -urN linux-2.4.21/grsecurity/grsec_disabled.c linux-2.4.21/grsecurity/grsec_disabled.c
7838--- linux-2.4.21/grsecurity/grsec_disabled.c 1969-12-31 19:00:00.000000000 -0500
7839+++ linux-2.4.21/grsecurity/grsec_disabled.c 2003-06-23 11:49:16.000000000 -0400
7840@@ -0,0 +1,368 @@
7841+/*
7842+ * when grsecurity is disabled, compile all external functions into nothing
7843+ */
7844+
7845+#include <linux/kernel.h>
7846+#include <linux/config.h>
7847+#include <linux/sched.h>
7848+#include <linux/file.h>
7849+#include <linux/fs.h>
7850+#include <linux/kdev_t.h>
7851+#include <linux/net.h>
7852+#include <linux/in.h>
7853+#include <linux/ip.h>
7854+#include <linux/skbuff.h>
7855+#include <linux/sysctl.h>
7856+
7857+#ifdef CONFIG_SYSCTL
7858+__u32
7859+gr_handle_sysctl(const struct ctl_table * table, __u32 mode)
7860+{
7861+ return mode;
7862+}
7863+#endif
7864+
7865+int
7866+gr_acl_is_enabled(void)
7867+{
7868+ return 0;
7869+}
7870+
7871+void
7872+gr_acl_handle_psacct(struct task_struct *task, const long code)
7873+{
7874+ return;
7875+}
7876+
7877+int
7878+gr_handle_ptrace_exec(const struct dentry *dentry, const struct vfsmount *mnt)
7879+{
7880+ return 0;
7881+}
7882+
7883+int
7884+gr_handle_mmap(const struct file *filp, const unsigned long prot)
7885+{
7886+ return 0;
7887+}
7888+
7889+int
7890+gr_handle_ptrace(struct task_struct *task, const long request)
7891+{
7892+ return 0;
7893+}
7894+
7895+void
7896+gr_learn_resource(const struct task_struct *task,
7897+ const int res, const unsigned long wanted)
7898+{
7899+ return;
7900+}
7901+
7902+int
7903+gr_set_acls(const int type)
7904+{
7905+ return 0;
7906+}
7907+
7908+int
7909+gr_check_hidden_task(const struct task_struct *tsk)
7910+{
7911+ return 0;
7912+}
7913+
7914+int
7915+gr_check_protected_task(const struct task_struct *task)
7916+{
7917+ return 0;
7918+}
7919+
7920+__inline__ void
7921+gr_copy_label(struct task_struct *tsk)
7922+{
7923+ return;
7924+}
7925+
7926+void
7927+gr_set_proc_label(const struct dentry *dentry, const struct vfsmount *mnt)
7928+{
7929+ return;
7930+}
7931+
7932+void
7933+gr_handle_delete(const ino_t ino, const kdev_t dev)
7934+{
7935+ return;
7936+}
7937+
7938+void
7939+gr_handle_create(const struct dentry *dentry, const struct vfsmount *mnt)
7940+{
7941+ return;
7942+}
7943+
7944+void
7945+gr_handle_crash(struct task_struct *task, const int sig)
7946+{
7947+ return;
7948+}
7949+
7950+int
7951+gr_check_crash_exec(const struct file *filp)
7952+{
7953+ return 0;
7954+}
7955+
7956+int
7957+gr_check_crash_uid(const uid_t uid)
7958+{
7959+ return 0;
7960+}
7961+
7962+int
7963+gr_handle_rename(struct inode *old_dir, struct inode *new_dir,
7964+ struct dentry *old_dentry,
7965+ struct dentry *new_dentry,
7966+ struct vfsmount *mnt, const __u8 replace)
7967+{
7968+ return 0;
7969+}
7970+
7971+int
7972+gr_search_socket(const int family, const int type, const int protocol)
7973+{
7974+ return 1;
7975+}
7976+
7977+int
7978+gr_search_connectbind(const int mode, const struct socket *sock,
7979+ const struct sockaddr_in *addr)
7980+{
7981+ return 1;
7982+}
7983+
7984+int
7985+gr_is_capable(const int cap)
7986+{
7987+ return 1;
7988+}
7989+
7990+void
7991+gr_handle_alertkill(void)
7992+{
7993+ return;
7994+}
7995+
7996+__u32
7997+gr_acl_handle_execve(const struct dentry * dentry, const struct vfsmount * mnt)
7998+{
7999+ return 1;
8000+}
8001+
8002+__u32
8003+gr_acl_handle_hidden_file(const struct dentry * dentry,
8004+ const struct vfsmount * mnt)
8005+{
8006+ return 1;
8007+}
8008+
8009+__u32
8010+gr_acl_handle_open(const struct dentry * dentry, const struct vfsmount * mnt,
8011+ const int fmode)
8012+{
8013+ return 1;
8014+}
8015+
8016+__u32
8017+gr_acl_handle_rmdir(const struct dentry * dentry, const struct vfsmount * mnt)
8018+{
8019+ return 1;
8020+}
8021+
8022+__u32
8023+gr_acl_handle_unlink(const struct dentry * dentry, const struct vfsmount * mnt)
8024+{
8025+ return 1;
8026+}
8027+
8028+int
8029+gr_acl_handle_mmap(const struct file *file, const unsigned long prot,
8030+ unsigned int *vm_flags)
8031+{
8032+ return 1;
8033+}
8034+
8035+__u32
8036+gr_acl_handle_truncate(const struct dentry * dentry,
8037+ const struct vfsmount * mnt)
8038+{
8039+ return 1;
8040+}
8041+
8042+__u32
8043+gr_acl_handle_utime(const struct dentry * dentry, const struct vfsmount * mnt)
8044+{
8045+ return 1;
8046+}
8047+
8048+__u32
8049+gr_acl_handle_access(const struct dentry * dentry,
8050+ const struct vfsmount * mnt, const int fmode)
8051+{
8052+ return 1;
8053+}
8054+
8055+__u32
8056+gr_acl_handle_fchmod(const struct dentry * dentry, const struct vfsmount * mnt,
8057+ mode_t mode)
8058+{
8059+ return 1;
8060+}
8061+
8062+__u32
8063+gr_acl_handle_chmod(const struct dentry * dentry, const struct vfsmount * mnt,
8064+ mode_t mode)
8065+{
8066+ return 1;
8067+}
8068+
8069+__u32
8070+gr_acl_handle_chown(const struct dentry * dentry, const struct vfsmount * mnt)
8071+{
8072+ return 1;
8073+}
8074+
8075+void
8076+grsecurity_init(void)
8077+{
8078+ return;
8079+}
8080+
8081+__u32
8082+gr_acl_handle_mknod(const struct dentry * new_dentry,
8083+ const struct dentry * parent_dentry,
8084+ const struct vfsmount * parent_mnt,
8085+ const int mode)
8086+{
8087+ return 1;
8088+}
8089+
8090+__u32
8091+gr_acl_handle_mkdir(const struct dentry * new_dentry,
8092+ const struct dentry * parent_dentry,
8093+ const struct vfsmount * parent_mnt)
8094+{
8095+ return 1;
8096+}
8097+
8098+__u32
8099+gr_acl_handle_symlink(const struct dentry * new_dentry,
8100+ const struct dentry * parent_dentry,
8101+ const struct vfsmount * parent_mnt, const char *from)
8102+{
8103+ return 1;
8104+}
8105+
8106+__u32
8107+gr_acl_handle_link(const struct dentry * new_dentry,
8108+ const struct dentry * parent_dentry,
8109+ const struct vfsmount * parent_mnt,
8110+ const struct dentry * old_dentry,
8111+ const struct vfsmount * old_mnt, const char *to)
8112+{
8113+ return 1;
8114+}
8115+
8116+int
8117+gr_acl_handle_rename(const struct dentry *new_dentry,
8118+ const struct dentry *parent_dentry,
8119+ const struct vfsmount *parent_mnt,
8120+ const struct dentry *old_dentry,
8121+ const struct inode *old_parent_inode,
8122+ const struct vfsmount *old_mnt, const char *newname)
8123+{
8124+ return 1;
8125+}
8126+
8127+__u32
8128+gr_acl_handle_filldir(const struct dentry * dentry,
8129+ const struct vfsmount * mnt, const ino_t ino)
8130+{
8131+ return 1;
8132+}
8133+
8134+int
8135+gr_handle_shmat(const pid_t shm_cprid, const pid_t shm_lapid,
8136+ const time_t shm_createtime, const uid_t cuid, const int shmid)
8137+{
8138+ return 1;
8139+}
8140+
8141+int
8142+gr_search_bind(const struct socket *sock, const struct sockaddr_in *addr)
8143+{
8144+ return 1;
8145+}
8146+
8147+int
8148+gr_search_connect(const struct socket *sock, const struct sockaddr_in *addr)
8149+{
8150+ return 1;
8151+}
8152+
8153+__u32
8154+gr_acl_handle_unix(const struct dentry * dentry, const struct vfsmount * mnt)
8155+{
8156+ return 1;
8157+}
8158+
8159+__u32
8160+gr_acl_handle_creat(const struct dentry * dentry,
8161+ const struct dentry * p_dentry,
8162+ const struct vfsmount * p_mnt, const int fmode,
8163+ const int imode)
8164+{
8165+ return 1;
8166+}
8167+
8168+void
8169+gr_acl_handle_exit(void)
8170+{
8171+ return;
8172+}
8173+
8174+int
8175+gr_acl_handle_mprotect(const struct file *file, const unsigned long prot)
8176+{
8177+ return 1;
8178+}
8179+
8180+void
8181+gr_set_role_label(const uid_t uid, const gid_t gid)
8182+{
8183+ return;
8184+}
8185+
8186+int
8187+gr_acl_handle_procpidmem(const struct task_struct *task)
8188+{
8189+ return 0;
8190+}
8191+
8192+int
8193+gr_search_udp_recvmsg(const struct sock *sk, const struct sk_buff *skb)
8194+{
8195+ return 1;
8196+}
8197+
8198+int
8199+gr_search_udp_sendmsg(const struct sock *sk, const struct sockaddr_in *addr)
8200+{
8201+ return 1;
8202+}
8203+
8204+void
8205+gr_set_kernel_label(struct task_struct *task)
8206+{
8207+ return;
8208+}
8209diff -urN linux-2.4.21/grsecurity/grsec_exec.c linux-2.4.21/grsecurity/grsec_exec.c
8210--- linux-2.4.21/grsecurity/grsec_exec.c 1969-12-31 19:00:00.000000000 -0500
8211+++ linux-2.4.21/grsecurity/grsec_exec.c 2003-06-23 11:49:16.000000000 -0400
8212@@ -0,0 +1,70 @@
8213+#include <linux/kernel.h>
8214+#include <linux/sched.h>
8215+#include <linux/file.h>
8216+#include <linux/fs.h>
8217+#include <linux/types.h>
8218+#include <linux/grdefs.h>
8219+#include <linux/grinternal.h>
8220+#include <linux/capability.h>
8221+
8222+#include <asm/uaccess.h>
8223+
8224+int
8225+gr_handle_nproc(void)
8226+{
8227+#ifdef CONFIG_GRKERNSEC_EXECVE
8228+ if (grsec_enable_execve && current->user &&
8229+ (atomic_read(&current->user->processes) >
8230+ current->rlim[RLIMIT_NPROC].rlim_cur) &&
8231+ !capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RESOURCE)) {
8232+ security_alert(GR_NPROC_MSG, DEFAULTSECARGS);
8233+ return -EAGAIN;
8234+ }
8235+#endif
8236+ return 0;
8237+}
8238+
8239+void
8240+gr_handle_exec_args(struct linux_binprm *bprm, char **argv)
8241+{
8242+#ifdef CONFIG_GRKERNSEC_EXECLOG
8243+ char grarg[64] = { 0 };
8244+ __u8 execlen = 0;
8245+ unsigned int i;
8246+
8247+ if (!((grsec_enable_execlog && grsec_enable_group &&
8248+ in_group_p(grsec_audit_gid))
8249+ || (grsec_enable_execlog && !grsec_enable_group)))
8250+ return;
8251+
8252+ if (unlikely(!argv))
8253+ goto log;
8254+
8255+ for (i = 0; i < bprm->argc && execlen < 62; i++) {
8256+ char *p;
8257+ __u8 len;
8258+
8259+ if (get_user(p, argv + i))
8260+ goto log;
8261+ if (!p)
8262+ goto log;
8263+ len = strnlen_user(p, 62 - execlen);
8264+ if (len > 62 - execlen)
8265+ len = 62 - execlen;
8266+ else if (len > 0)
8267+ len--;
8268+ if (copy_from_user(grarg + execlen, p, len))
8269+ goto log;
8270+ execlen += len;
8271+ *(grarg + execlen) = ' ';
8272+ *(grarg + execlen + 1) = '\0';
8273+ execlen++;
8274+ }
8275+
8276+ log:
8277+ security_audit(GR_EXEC_AUDIT_MSG, gr_to_filename(bprm->file->f_dentry,
8278+ bprm->file->f_vfsmnt),
8279+ grarg, DEFAULTSECARGS);
8280+#endif
8281+ return;
8282+}
8283diff -urN linux-2.4.21/grsecurity/grsec_fifo.c linux-2.4.21/grsecurity/grsec_fifo.c
8284--- linux-2.4.21/grsecurity/grsec_fifo.c 1969-12-31 19:00:00.000000000 -0500
8285+++ linux-2.4.21/grsecurity/grsec_fifo.c 2003-06-23 11:49:16.000000000 -0400
8286@@ -0,0 +1,24 @@
8287+#include <linux/kernel.h>
8288+#include <linux/sched.h>
8289+#include <linux/fs.h>
8290+#include <linux/file.h>
8291+#include <linux/grinternal.h>
8292+
8293+int
8294+gr_handle_fifo(const struct dentry *dentry, const struct vfsmount *mnt,
8295+ const struct dentry *dir, const int flag, const int acc_mode)
8296+{
8297+#ifdef CONFIG_GRKERNSEC_FIFO
8298+ if (grsec_enable_fifo && S_ISFIFO(dentry->d_inode->i_mode) &&
8299+ !(flag & O_EXCL) && (dir->d_inode->i_mode & S_ISVTX) &&
8300+ (dentry->d_inode->i_uid != dir->d_inode->i_uid) &&
8301+ (current->fsuid != dentry->d_inode->i_uid)) {
8302+ if (!permission(dentry->d_inode, acc_mode))
8303+ security_alert(GR_FIFO_MSG, gr_to_filename(dentry, mnt),
8304+ dentry->d_inode->i_uid,
8305+ dentry->d_inode->i_gid, DEFAULTSECARGS);
8306+ return -EACCES;
8307+ }
8308+#endif
8309+ return 0;
8310+}
8311diff -urN linux-2.4.21/grsecurity/grsec_fork.c linux-2.4.21/grsecurity/grsec_fork.c
8312--- linux-2.4.21/grsecurity/grsec_fork.c 1969-12-31 19:00:00.000000000 -0500
8313+++ linux-2.4.21/grsecurity/grsec_fork.c 2003-06-23 11:49:16.000000000 -0400
8314@@ -0,0 +1,14 @@
8315+#include <linux/kernel.h>
8316+#include <linux/sched.h>
8317+#include <linux/grsecurity.h>
8318+#include <linux/grinternal.h>
8319+
8320+void
8321+gr_log_forkfail(const int retval)
8322+{
8323+#ifdef CONFIG_GRKERNSEC_FORKFAIL
8324+ if (grsec_enable_forkfail)
8325+ security_alert(GR_FAILFORK_MSG, retval, DEFAULTSECARGS);
8326+#endif
8327+ return;
8328+}
8329diff -urN linux-2.4.21/grsecurity/grsec_init.c linux-2.4.21/grsecurity/grsec_init.c
8330--- linux-2.4.21/grsecurity/grsec_init.c 1969-12-31 19:00:00.000000000 -0500
8331+++ linux-2.4.21/grsecurity/grsec_init.c 2003-06-23 11:49:16.000000000 -0400
8332@@ -0,0 +1,203 @@
8333+#include <linux/kernel.h>
8334+#include <linux/sched.h>
8335+#include <linux/mm.h>
8336+#include <linux/smp_lock.h>
8337+
8338+int grsec_enable_link;
8339+int grsec_enable_dmesg;
8340+int grsec_enable_fifo;
8341+int grsec_enable_execve;
8342+int grsec_enable_execlog;
8343+int grsec_enable_signal;
8344+int grsec_enable_forkfail;
8345+int grsec_enable_time;
8346+int grsec_enable_group;
8347+int grsec_audit_gid;
8348+int grsec_enable_chdir;
8349+int grsec_enable_audit_ipc;
8350+int grsec_enable_mount;
8351+int grsec_enable_chroot_findtask;
8352+int grsec_enable_chroot_mount;
8353+int grsec_enable_chroot_shmat;
8354+int grsec_enable_chroot_fchdir;
8355+int grsec_enable_chroot_double;
8356+int grsec_enable_chroot_pivot;
8357+int grsec_enable_chroot_chdir;
8358+int grsec_enable_chroot_chmod;
8359+int grsec_enable_chroot_mknod;
8360+int grsec_enable_chroot_nice;
8361+int grsec_enable_chroot_execlog;
8362+int grsec_enable_chroot_caps;
8363+int grsec_enable_chroot_sysctl;
8364+int grsec_enable_chroot_unix;
8365+int grsec_enable_tpe;
8366+int grsec_tpe_gid;
8367+int grsec_enable_tpe_all;
8368+int grsec_enable_randpid;
8369+int grsec_enable_randid;
8370+int grsec_enable_randisn;
8371+int grsec_enable_randsrc;
8372+int grsec_enable_randrpc;
8373+int grsec_enable_randping;
8374+int grsec_enable_socket_all;
8375+int grsec_socket_all_gid;
8376+int grsec_enable_socket_client;
8377+int grsec_socket_client_gid;
8378+int grsec_enable_socket_server;
8379+int grsec_socket_server_gid;
8380+int grsec_lock;
8381+
8382+spinlock_t grsec_alert_lock = SPIN_LOCK_UNLOCKED;
8383+unsigned long grsec_alert_wtime = 0;
8384+unsigned long grsec_alert_fyet = 0;
8385+
8386+spinlock_t grsec_alertgood_lock = SPIN_LOCK_UNLOCKED;
8387+unsigned long grsec_alertgood_wtime = 0;
8388+unsigned long grsec_alertgood_fyet = 0;
8389+
8390+spinlock_t grsec_audit_lock = SPIN_LOCK_UNLOCKED;
8391+
8392+char *gr_shared_page[2][NR_CPUS];
8393+
8394+extern void arc4_init(void);
8395+
8396+void
8397+grsecurity_init(void)
8398+{
8399+ int i, j;
8400+ /* create the per-cpu shared pages */
8401+
8402+ for (j = 0; j < 2; j++) {
8403+ for (i = 0; i < NR_CPUS; i++) {
8404+ gr_shared_page[j][i] = (char *) get_zeroed_page(GFP_KERNEL);
8405+ if (!gr_shared_page[j][i]) {
8406+ panic("Unable to allocate grsecurity shared page");
8407+ return;
8408+ }
8409+ }
8410+ }
8411+
8412+ arc4_init();
8413+
8414+#ifndef CONFIG_GRKERNSEC_SYSCTL
8415+ grsec_lock = 1;
8416+#ifdef CONFIG_GRKERNSEC_AUDIT_GROUP
8417+ grsec_enable_group = 1;
8418+ grsec_audit_gid = CONFIG_GRKERNSEC_AUDIT_GID;
8419+#endif
8420+#ifdef CONFIG_GRKERNSEC_AUDIT_CHDIR
8421+ grsec_enable_chdir = 1;
8422+#endif
8423+#ifdef CONFIG_GRKERNSEC_AUDIT_IPC
8424+ grsec_enable_audit_ipc = 1;
8425+#endif
8426+#ifdef CONFIG_GRKERNSEC_AUDIT_MOUNT
8427+ grsec_enable_mount = 1;
8428+#endif
8429+#ifdef CONFIG_GRKERNSEC_LINK
8430+ grsec_enable_link = 1;
8431+#endif
8432+#ifdef CONFIG_GRKERNSEC_DMESG
8433+ grsec_enable_dmesg = 1;
8434+#endif
8435+#ifdef CONFIG_GRKERNSEC_FIFO
8436+ grsec_enable_fifo = 1;
8437+#endif
8438+#ifdef CONFIG_GRKERNSEC_EXECVE
8439+ grsec_enable_execve = 1;
8440+#endif
8441+#ifdef CONFIG_GRKERNSEC_EXECLOG
8442+ grsec_enable_execlog = 1;
8443+#endif
8444+#ifdef CONFIG_GRKERNSEC_SIGNAL
8445+ grsec_enable_signal = 1;
8446+#endif
8447+#ifdef CONFIG_GRKERNSEC_FORKFAIL
8448+ grsec_enable_forkfail = 1;
8449+#endif
8450+#ifdef CONFIG_GRKERNSEC_TIME
8451+ grsec_enable_time = 1;
8452+#endif
8453+#ifdef CONFIG_GRKERNSEC_CHROOT_FINDTASK
8454+ grsec_enable_chroot_findtask = 1;
8455+#endif
8456+#ifdef CONFIG_GRKERNSEC_CHROOT_UNIX
8457+ grsec_enable_chroot_unix = 1;
8458+#endif
8459+#ifdef CONFIG_GRKERNSEC_CHROOT_MOUNT
8460+ grsec_enable_chroot_mount = 1;
8461+#endif
8462+#ifdef CONFIG_GRKERNSEC_CHROOT_FCHDIR
8463+ grsec_enable_chroot_fchdir = 1;
8464+#endif
8465+#ifdef CONFIG_GRKERNSEC_CHROOT_SHMAT
8466+ grsec_enable_chroot_shmat = 1;
8467+#endif
8468+#ifdef CONFIG_GRKERNSEC_CHROOT_DOUBLE
8469+ grsec_enable_chroot_double = 1;
8470+#endif
8471+#ifdef CONFIG_GRKERNSEC_CHROOT_PIVOT
8472+ grsec_enable_chroot_pivot = 1;
8473+#endif
8474+#ifdef CONFIG_GRKERNSEC_CHROOT_CHDIR
8475+ grsec_enable_chroot_chdir = 1;
8476+#endif
8477+#ifdef CONFIG_GRKERNSEC_CHROOT_CHMOD
8478+ grsec_enable_chroot_chmod = 1;
8479+#endif
8480+#ifdef CONFIG_GRKERNSEC_CHROOT_MKNOD
8481+ grsec_enable_chroot_mknod = 1;
8482+#endif
8483+#ifdef CONFIG_GRKERNSEC_CHROOT_NICE
8484+ grsec_enable_chroot_nice = 1;
8485+#endif
8486+#ifdef CONFIG_GRKERNSEC_CHROOT_EXECLOG
8487+ grsec_enable_chroot_execlog = 1;
8488+#endif
8489+#ifdef CONFIG_GRKERNSEC_CHROOT_CAPS
8490+ grsec_enable_chroot_caps = 1;
8491+#endif
8492+#ifdef CONFIG_GRKERNSEC_CHROOT_SYSCTL
8493+ grsec_enable_chroot_sysctl = 1;
8494+#endif
8495+#ifdef CONFIG_GRKERNSEC_TPE
8496+ grsec_enable_tpe = 1;
8497+ grsec_tpe_gid = CONFIG_GRKERNSEC_TPE_GID;
8498+#ifdef CONFIG_GRKERNSEC_TPE_ALL
8499+ grsec_enable_tpe_all = 1;
8500+#endif
8501+#endif
8502+#ifdef CONFIG_GRKERNSEC_RANDPID
8503+ grsec_enable_randpid = 1;
8504+#endif
8505+#ifdef CONFIG_GRKERNSEC_RANDID
8506+ grsec_enable_randid = 1;
8507+#endif
8508+#ifdef CONFIG_GRKERNSEC_RANDISN
8509+ grsec_enable_randisn = 1;
8510+#endif
8511+#ifdef CONFIG_GRKERNSEC_RANDSRC
8512+ grsec_enable_randsrc = 1;
8513+#endif
8514+#ifdef CONFIG_GRKERNSEC_RANDRPC
8515+ grsec_enable_randrpc = 1;
8516+#endif
8517+#ifdef CONFIG_GRKERNSEC_RANDPING
8518+ grsec_enable_randping = 1;
8519+#endif
8520+#ifdef CONFIG_GRKERNSEC_SOCKET_ALL
8521+ grsec_enable_socket_all = 1;
8522+ grsec_socket_all_gid = CONFIG_GRKERNSEC_SOCKET_ALL_GID;
8523+#endif
8524+#ifdef CONFIG_GRKERNSEC_SOCKET_CLIENT
8525+ grsec_enable_socket_client = 1;
8526+ grsec_socket_client_gid = CONFIG_GRKERNSEC_SOCKET_CLIENT_GID;
8527+#endif
8528+#ifdef CONFIG_GRKERNSEC_SOCKET_SERVER
8529+ grsec_enable_socket_server = 1;
8530+ grsec_socket_server_gid = CONFIG_GRKERNSEC_SOCKET_SERVER_GID;
8531+#endif
8532+#endif
8533+
8534+ return;
8535+}
8536diff -urN linux-2.4.21/grsecurity/grsec_ipc.c linux-2.4.21/grsecurity/grsec_ipc.c
8537--- linux-2.4.21/grsecurity/grsec_ipc.c 1969-12-31 19:00:00.000000000 -0500
8538+++ linux-2.4.21/grsecurity/grsec_ipc.c 2003-06-23 11:49:16.000000000 -0400
8539@@ -0,0 +1,81 @@
8540+#include <linux/kernel.h>
8541+#include <linux/sched.h>
8542+#include <linux/types.h>
8543+#include <linux/ipc.h>
8544+#include <linux/grsecurity.h>
8545+#include <linux/grinternal.h>
8546+
8547+void
8548+gr_log_msgget(const int ret, const int msgflg)
8549+{
8550+#ifdef CONFIG_GRKERNSEC_AUDIT_IPC
8551+ if (((grsec_enable_group && in_group_p(grsec_audit_gid) &&
8552+ grsec_enable_audit_ipc) || (grsec_enable_audit_ipc &&
8553+ !grsec_enable_group)) && (ret >= 0)
8554+ && (msgflg & IPC_CREAT))
8555+ security_audit(GR_MSGQ_AUDIT_MSG, DEFAULTSECARGS);
8556+#endif
8557+ return;
8558+}
8559+
8560+void
8561+gr_log_msgrm(const uid_t uid, const uid_t cuid)
8562+{
8563+#ifdef CONFIG_GRKERNSEC_AUDIT_IPC
8564+ if ((grsec_enable_group && in_group_p(grsec_audit_gid) &&
8565+ grsec_enable_audit_ipc) ||
8566+ (grsec_enable_audit_ipc && !grsec_enable_group))
8567+ security_audit(GR_MSGQR_AUDIT_MSG, uid, cuid, DEFAULTSECARGS);
8568+#endif
8569+ return;
8570+}
8571+
8572+void
8573+gr_log_semget(const int err, const int semflg)
8574+{
8575+#ifdef CONFIG_GRKERNSEC_AUDIT_IPC
8576+ if (((grsec_enable_group && in_group_p(grsec_audit_gid) &&
8577+ grsec_enable_audit_ipc) || (grsec_enable_audit_ipc &&
8578+ !grsec_enable_group)) && (err >= 0)
8579+ && (semflg & IPC_CREAT))
8580+ security_audit(GR_SEM_AUDIT_MSG, DEFAULTSECARGS);
8581+#endif
8582+ return;
8583+}
8584+
8585+void
8586+gr_log_semrm(const uid_t uid, const uid_t cuid)
8587+{
8588+#ifdef CONFIG_GRKERNSEC_AUDIT_IPC
8589+ if ((grsec_enable_group && in_group_p(grsec_audit_gid) &&
8590+ grsec_enable_audit_ipc) ||
8591+ (grsec_enable_audit_ipc && !grsec_enable_group))
8592+ security_audit(GR_SEMR_AUDIT_MSG, uid, cuid, DEFAULTSECARGS);
8593+#endif
8594+ return;
8595+}
8596+
8597+void
8598+gr_log_shmget(const int err, const int shmflg, const size_t size)
8599+{
8600+#ifdef CONFIG_GRKERNSEC_AUDIT_IPC
8601+ if (((grsec_enable_group && in_group_p(grsec_audit_gid) &&
8602+ grsec_enable_audit_ipc) || (grsec_enable_audit_ipc &&
8603+ !grsec_enable_group)) && (err >= 0)
8604+ && (shmflg & IPC_CREAT))
8605+ security_audit(GR_SHM_AUDIT_MSG, size, DEFAULTSECARGS);
8606+#endif
8607+ return;
8608+}
8609+
8610+void
8611+gr_log_shmrm(const uid_t uid, const uid_t cuid)
8612+{
8613+#ifdef CONFIG_GRKERNSEC_AUDIT_IPC
8614+ if ((grsec_enable_group && in_group_p(grsec_audit_gid) &&
8615+ grsec_enable_audit_ipc) ||
8616+ (grsec_enable_audit_ipc && !grsec_enable_group))
8617+ security_audit(GR_SHMR_AUDIT_MSG, uid, cuid, DEFAULTSECARGS);
8618+#endif
8619+ return;
8620+}
8621diff -urN linux-2.4.21/grsecurity/grsec_link.c linux-2.4.21/grsecurity/grsec_link.c
8622--- linux-2.4.21/grsecurity/grsec_link.c 1969-12-31 19:00:00.000000000 -0500
8623+++ linux-2.4.21/grsecurity/grsec_link.c 2003-06-23 11:49:16.000000000 -0400
8624@@ -0,0 +1,41 @@
8625+#include <linux/kernel.h>
8626+#include <linux/sched.h>
8627+#include <linux/fs.h>
8628+#include <linux/file.h>
8629+#include <linux/grinternal.h>
8630+
8631+int
8632+gr_handle_follow_link(const struct inode *parent,
8633+ const struct inode *inode,
8634+ const struct dentry *dentry, const struct vfsmount *mnt)
8635+{
8636+#ifdef CONFIG_GRKERNSEC_LINK
8637+ if (grsec_enable_link && S_ISLNK(inode->i_mode) &&
8638+ (parent->i_mode & S_ISVTX) && (parent->i_uid != inode->i_uid) &&
8639+ (parent->i_mode & S_IWOTH) && (current->fsuid != inode->i_uid)) {
8640+ security_alert(GR_SYMLINK_MSG, gr_to_filename(dentry, mnt),
8641+ inode->i_uid, inode->i_gid, DEFAULTSECARGS);
8642+ return -EACCES;
8643+ }
8644+#endif
8645+ return 0;
8646+}
8647+
8648+int
8649+gr_handle_hardlink(const struct dentry *dentry,
8650+ const struct vfsmount *mnt,
8651+ struct inode *inode, const int mode, const char *to)
8652+{
8653+#ifdef CONFIG_GRKERNSEC_LINK
8654+ if (grsec_enable_link && current->fsuid != inode->i_uid &&
8655+ (!S_ISREG(mode) || (mode & S_ISUID) ||
8656+ ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) ||
8657+ (permission(inode, MAY_READ | MAY_WRITE))) &&
8658+ !capable(CAP_FOWNER) && current->uid) {
8659+ security_alert(GR_HARDLINK_MSG, gr_to_filename(dentry, mnt),
8660+ inode->i_uid, inode->i_gid, to, DEFAULTSECARGS);
8661+ return -EPERM;
8662+ }
8663+#endif
8664+ return 0;
8665+}
8666diff -urN linux-2.4.21/grsecurity/grsec_mem.c linux-2.4.21/grsecurity/grsec_mem.c
8667--- linux-2.4.21/grsecurity/grsec_mem.c 1969-12-31 19:00:00.000000000 -0500
8668+++ linux-2.4.21/grsecurity/grsec_mem.c 2003-06-23 11:49:16.000000000 -0400
8669@@ -0,0 +1,54 @@
8670+#include <linux/kernel.h>
8671+#include <linux/sched.h>
8672+#include <linux/mm.h>
8673+#include <linux/mman.h>
8674+#include <linux/grinternal.h>
8675+
8676+void
8677+gr_handle_ioperm(void)
8678+{
8679+ security_alert(GR_IOPERM_MSG, DEFAULTSECARGS);
8680+ return;
8681+}
8682+
8683+void
8684+gr_handle_iopl(void)
8685+{
8686+ security_alert(GR_IOPL_MSG, DEFAULTSECARGS);
8687+ return;
8688+}
8689+
8690+void
8691+gr_handle_mem_write(void)
8692+{
8693+ security_alert(GR_MEM_WRITE_MSG, DEFAULTSECARGS);
8694+ return;
8695+}
8696+
8697+void
8698+gr_handle_kmem_write(void)
8699+{
8700+ security_alert(GR_KMEM_MSG, DEFAULTSECARGS);
8701+ return;
8702+}
8703+
8704+void
8705+gr_handle_open_port(void)
8706+{
8707+ security_alert(GR_PORT_OPEN_MSG, DEFAULTSECARGS);
8708+ return;
8709+}
8710+
8711+int
8712+gr_handle_mem_mmap(const unsigned long offset, struct vm_area_struct *vma)
8713+{
8714+ if (offset < __pa(high_memory) &&
8715+ (pgprot_val(vma->vm_page_prot) & PROT_WRITE) &&
8716+ (offset != 0xa0000 || ((vma->vm_end - vma->vm_start) > 0x20000))) {
8717+ security_alert(GR_MEM_MMAP_MSG, DEFAULTSECARGS);
8718+ return -EPERM;
8719+ } else if (offset < __pa(high_memory))
8720+ vma->vm_flags &= ~VM_MAYWRITE;
8721+
8722+ return 0;
8723+}
8724diff -urN linux-2.4.21/grsecurity/grsec_mount.c linux-2.4.21/grsecurity/grsec_mount.c
8725--- linux-2.4.21/grsecurity/grsec_mount.c 1969-12-31 19:00:00.000000000 -0500
8726+++ linux-2.4.21/grsecurity/grsec_mount.c 2003-06-23 11:49:16.000000000 -0400
8727@@ -0,0 +1,34 @@
8728+#include <linux/kernel.h>
8729+#include <linux/sched.h>
8730+#include <linux/grsecurity.h>
8731+#include <linux/grinternal.h>
8732+
8733+void
8734+gr_log_remount(const char *devname, const int retval)
8735+{
8736+#ifdef CONFIG_GRKERNSEC_AUDIT_MOUNT
8737+ if (grsec_enable_mount && (retval >= 0))
8738+ security_audit(GR_REMOUNT_AUDIT_MSG, devname, DEFAULTSECARGS);
8739+#endif
8740+ return;
8741+}
8742+
8743+void
8744+gr_log_unmount(const char *devname, const int retval)
8745+{
8746+#ifdef CONFIG_GRKERNSEC_AUDIT_MOUNT
8747+ if (grsec_enable_mount && (retval >= 0))
8748+ security_audit(GR_UNMOUNT_AUDIT_MSG, devname, DEFAULTSECARGS);
8749+#endif
8750+ return;
8751+}
8752+
8753+void
8754+gr_log_mount(const char *from, const char *to, const int retval)
8755+{
8756+#ifdef CONFIG_GRKERNSEC_AUDIT_MOUNT
8757+ if (grsec_enable_mount && (retval >= 0))
8758+ security_audit(GR_MOUNT_AUDIT_MSG, from, to, DEFAULTSECARGS);
8759+#endif
8760+ return;
8761+}
8762diff -urN linux-2.4.21/grsecurity/grsec_rand.c linux-2.4.21/grsecurity/grsec_rand.c
8763--- linux-2.4.21/grsecurity/grsec_rand.c 1969-12-31 19:00:00.000000000 -0500
8764+++ linux-2.4.21/grsecurity/grsec_rand.c 2003-06-23 11:49:16.000000000 -0400
8765@@ -0,0 +1,36 @@
8766+#include <linux/kernel.h>
8767+#include <linux/sched.h>
8768+#include <linux/smp_lock.h>
8769+#include <linux/grsecurity.h>
8770+#include <linux/grinternal.h>
8771+
8772+extern int last_pid;
8773+
8774+int
8775+gr_random_pid(spinlock_t * pid_lock)
8776+{
8777+#ifdef CONFIG_GRKERNSEC_RANDPID
8778+ struct task_struct *p;
8779+ int pid;
8780+
8781+ if (grsec_enable_randpid && current->fs->root) {
8782+ read_lock(&tasklist_lock);
8783+ spin_lock(pid_lock);
8784+
8785+ repeater:
8786+
8787+ pid = 1 + (arc4random() % PID_MAX);
8788+
8789+ for_each_task(p) {
8790+ if (p->pid == pid || p->pgrp == pid ||
8791+ p->tgid == pid || p->session == pid)
8792+ goto repeater;
8793+ }
8794+ last_pid = pid;
8795+ spin_unlock(pid_lock);
8796+ read_unlock(&tasklist_lock);
8797+ return pid;
8798+ }
8799+#endif
8800+ return 0;
8801+}
8802diff -urN linux-2.4.21/grsecurity/grsec_sig.c linux-2.4.21/grsecurity/grsec_sig.c
8803--- linux-2.4.21/grsecurity/grsec_sig.c 1969-12-31 19:00:00.000000000 -0500
8804+++ linux-2.4.21/grsecurity/grsec_sig.c 2003-06-23 11:49:16.000000000 -0400
8805@@ -0,0 +1,43 @@
8806+#include <linux/kernel.h>
8807+#include <linux/sched.h>
8808+#include <linux/grinternal.h>
8809+
8810+void
8811+gr_log_signal(const int sig, const struct task_struct *t)
8812+{
8813+#ifdef CONFIG_GRKERNSEC_SIGNAL
8814+ if (grsec_enable_signal && ((sig == SIGSEGV) || (sig == SIGILL) ||
8815+ (sig == SIGABRT) || (sig == SIGBUS))) {
8816+ if (t->pid == current->pid) {
8817+ security_alert_good(GR_UNISIGLOG_MSG, sig,
8818+ DEFAULTSECARGS);
8819+ } else {
8820+ security_alert_good(GR_DUALSIGLOG_MSG, sig, t->comm,
8821+ t->pid, t->uid, t->euid, t->gid,
8822+ t->egid, t->p_pptr->comm,
8823+ t->p_pptr->pid, t->p_pptr->uid,
8824+ t->p_pptr->euid, t->p_pptr->gid,
8825+ t->p_pptr->egid, DEFAULTSECARGS);
8826+ }
8827+ }
8828+#endif
8829+ return;
8830+}
8831+
8832+int
8833+gr_handle_signal(const struct task_struct *p, const int sig)
8834+{
8835+#ifdef CONFIG_GRKERNSEC
8836+ if (current->pid > 1 && gr_check_protected_task(p)) {
8837+ security_alert(GR_SIG_ACL_MSG, sig, p->comm, p->pid, p->uid,
8838+ p->euid, p->gid, p->egid, p->p_pptr->comm,
8839+ p->p_pptr->pid, p->p_pptr->uid,
8840+ p->p_pptr->euid, p->p_pptr->gid,
8841+ p->p_pptr->egid, DEFAULTSECARGS);
8842+ return -EPERM;
8843+ } else if (gr_pid_is_chrooted(p)) {
8844+ return -EPERM;
8845+ }
8846+#endif
8847+ return 0;
8848+}
8849diff -urN linux-2.4.21/grsecurity/grsec_sock.c linux-2.4.21/grsecurity/grsec_sock.c
8850--- linux-2.4.21/grsecurity/grsec_sock.c 1969-12-31 19:00:00.000000000 -0500
8851+++ linux-2.4.21/grsecurity/grsec_sock.c 2003-06-23 11:49:16.000000000 -0400
8852@@ -0,0 +1,123 @@
8853+#include <linux/kernel.h>
8854+#include <linux/sched.h>
8855+#include <linux/file.h>
8856+#include <linux/net.h>
8857+#include <net/sock.h>
8858+#include <linux/grsecurity.h>
8859+#include <linux/grinternal.h>
8860+#include <linux/gracl.h>
8861+
8862+void
8863+gr_attach_curr_ip(const struct sock *sk)
8864+{
8865+#ifdef CONFIG_GRKERNSEC
8866+ struct task_struct *p;
8867+ unsigned int i;
8868+ struct inode *inode;
8869+ struct file *filp;
8870+ struct socket *connect_sock;
8871+
8872+ if (unlikely(sk->protocol != IPPROTO_TCP))
8873+ return;
8874+
8875+ read_lock(&tasklist_lock);
8876+ for_each_task(p) {
8877+ if (!p->used_connect)
8878+ continue;
8879+ task_lock(p);
8880+ if (unlikely(!p->files)) {
8881+ task_unlock(p);
8882+ continue;
8883+ }
8884+ read_lock(&p->files->file_lock);
8885+ for (i = 0; i < p->files->max_fds; i++) {
8886+ filp = fcheck_files(p->files, i);
8887+ if (likely(!filp))
8888+ continue;
8889+ inode = filp->f_dentry->d_inode;
8890+ if (likely(!inode || !inode->i_sock))
8891+ continue;
8892+ connect_sock = &inode->u.socket_i;
8893+ if (unlikely(!connect_sock ||
8894+ connect_sock->sk->protocol != IPPROTO_TCP))
8895+ continue;
8896+ if (unlikely(sk->rcv_saddr == connect_sock->sk->daddr &&
8897+ sk->daddr == connect_sock->sk->rcv_saddr &&
8898+ ntohs(sk->sport) ==
8899+ ntohs(connect_sock->sk->dport)
8900+ && ntohs(sk->dport) ==
8901+ ntohs(connect_sock->sk->sport))) {
8902+ current->curr_ip = p->curr_ip;
8903+ current->used_accept = 1;
8904+ read_unlock(&p->files->file_lock);
8905+ task_unlock(p);
8906+ read_unlock(&tasklist_lock);
8907+ return;
8908+ }
8909+ }
8910+ read_unlock(&p->files->file_lock);
8911+ task_unlock(p);
8912+ }
8913+ read_unlock(&tasklist_lock);
8914+
8915+ current->curr_ip = sk->daddr;
8916+ current->used_accept = 1;
8917+#endif
8918+ return;
8919+}
8920+
8921+int
8922+gr_handle_sock_all(const int family, const int type, const int protocol)
8923+{
8924+#ifdef CONFIG_GRKERNSEC_SOCKET_ALL
8925+ if (grsec_enable_socket_all && in_group_p(grsec_socket_all_gid) &&
8926+ (family != AF_UNIX) && (family != AF_LOCAL)) {
8927+ security_alert(GR_SOCK_MSG, family, type, protocol,
8928+ DEFAULTSECARGS);
8929+ return -EACCES;
8930+ }
8931+#endif
8932+ return 0;
8933+}
8934+
8935+int
8936+gr_handle_sock_server(const struct sockaddr *sck)
8937+{
8938+#ifdef CONFIG_GRKERNSEC_SOCKET_SERVER
8939+ if (grsec_enable_socket_server &&
8940+ in_group_p(grsec_socket_server_gid) &&
8941+ sck && (sck->sa_family != AF_UNIX) &&
8942+ (sck->sa_family != AF_LOCAL)) {
8943+ security_alert(GR_BIND_MSG, DEFAULTSECARGS);
8944+ return -EACCES;
8945+ }
8946+#endif
8947+ return 0;
8948+}
8949+
8950+int
8951+gr_handle_sock_client(const struct sockaddr *sck)
8952+{
8953+#ifdef CONFIG_GRKERNSEC_SOCKET_CLIENT
8954+ if (grsec_enable_socket_client && in_group_p(grsec_socket_client_gid) &&
8955+ sck && (sck->sa_family != AF_UNIX) &&
8956+ (sck->sa_family != AF_LOCAL)) {
8957+ security_alert(GR_CONNECT_MSG, DEFAULTSECARGS);
8958+ return -EACCES;
8959+ }
8960+#endif
8961+ return 0;
8962+}
8963+
8964+__u32
8965+gr_cap_rtnetlink(void)
8966+{
8967+#ifdef CONFIG_GRKERNSEC
8968+ if (!gr_acl_is_enabled())
8969+ return current->cap_effective;
8970+ else
8971+ return (current->cap_effective & ~(current->acl->cap_lower));
8972+#else
8973+ return current->cap_effective;
8974+#endif
8975+}
8976diff -urN linux-2.4.21/grsecurity/grsec_sysctl.c linux-2.4.21/grsecurity/grsec_sysctl.c
8977--- linux-2.4.21/grsecurity/grsec_sysctl.c 1969-12-31 19:00:00.000000000 -0500
8978+++ linux-2.4.21/grsecurity/grsec_sysctl.c 2003-06-23 11:49:16.000000000 -0400
8979@@ -0,0 +1,16 @@
8980+#include <linux/kernel.h>
8981+#include <linux/sched.h>
8982+#include <linux/sysctl.h>
8983+#include <linux/grinternal.h>
8984+
8985+int
8986+gr_handle_sysctl_mod(const char *dirname, const char *name, const int op)
8987+{
8988+#ifdef CONFIG_GRKERNSEC_SYSCTL
8989+ if (!strcmp(dirname, "grsecurity") && grsec_lock && (op & 002)) {
8990+ security_alert(GR_SYSCTL_MSG, name, DEFAULTSECARGS);
8991+ return -EACCES;
8992+ }
8993+#endif
8994+ return 0;
8995+}
8996diff -urN linux-2.4.21/grsecurity/grsec_time.c linux-2.4.21/grsecurity/grsec_time.c
8997--- linux-2.4.21/grsecurity/grsec_time.c 1969-12-31 19:00:00.000000000 -0500
8998+++ linux-2.4.21/grsecurity/grsec_time.c 2003-06-23 11:49:16.000000000 -0400
8999@@ -0,0 +1,13 @@
9000+#include <linux/kernel.h>
9001+#include <linux/sched.h>
9002+#include <linux/grinternal.h>
9003+
9004+void
9005+gr_log_timechange(void)
9006+{
9007+#ifdef CONFIG_GRKERNSEC_TIME
9008+ if (grsec_enable_time)
9009+ security_alert_good(GR_TIME_MSG, DEFAULTSECARGS);
9010+#endif
9011+ return;
9012+}
9013diff -urN linux-2.4.21/grsecurity/grsec_tpe.c linux-2.4.21/grsecurity/grsec_tpe.c
9014--- linux-2.4.21/grsecurity/grsec_tpe.c 1969-12-31 19:00:00.000000000 -0500
9015+++ linux-2.4.21/grsecurity/grsec_tpe.c 2003-06-23 11:49:16.000000000 -0400
9016@@ -0,0 +1,33 @@
9017+#include <linux/kernel.h>
9018+#include <linux/sched.h>
9019+#include <linux/file.h>
9020+#include <linux/fs.h>
9021+#include <linux/grinternal.h>
9022+
9023+int
9024+gr_tpe_allow(const struct file *file)
9025+{
9026+#ifdef CONFIG_GRKERNSEC_TPE
9027+ struct inode *inode = file->f_dentry->d_parent->d_inode;
9028+
9029+ if (current->uid && grsec_enable_tpe && in_group_p(grsec_tpe_gid) &&
9030+ (inode->i_uid || (!inode->i_uid && ((inode->i_mode & S_IWGRP) ||
9031+ (inode->i_mode & S_IWOTH))))) {
9032+ security_alert(GR_EXEC_TPE_MSG,
9033+ gr_to_filename(file->f_dentry, file->f_vfsmnt),
9034+ DEFAULTSECARGS);
9035+ return 0;
9036+ }
9037+#ifdef CONFIG_GRKERNSEC_TPE_ALL
9038+ if (current->uid && grsec_enable_tpe && grsec_enable_tpe_all &&
9039+ ((inode->i_uid && (inode->i_uid != current->uid)) ||
9040+ (inode->i_mode & S_IWGRP) || (inode->i_mode & S_IWOTH))) {
9041+ security_alert(GR_EXEC_TPE_MSG,
9042+ gr_to_filename(file->f_dentry, file->f_vfsmnt),
9043+ DEFAULTSECARGS);
9044+ return 0;
9045+ }
9046+#endif
9047+#endif
9048+ return 1;
9049+}
9050diff -urN linux-2.4.21/grsecurity/grsum.c linux-2.4.21/grsecurity/grsum.c
9051--- linux-2.4.21/grsecurity/grsum.c 1969-12-31 19:00:00.000000000 -0500
9052+++ linux-2.4.21/grsecurity/grsum.c 2003-06-23 11:49:16.000000000 -0400
9053@@ -0,0 +1,297 @@
9054+#include <linux/kernel.h>
9055+#include <linux/sched.h>
9056+#include <linux/gracl.h>
9057+
9058+/* digest-sha256.c,v 1.13 2002/10/02 22:02:08 hvr Exp $
9059+ *
9060+ * SHA-256 code by Jean-Luc Cooke <jlcooke@certainkey.com>.
9061+ *
9062+ * Glue code originally by Andrew McDonald and Alan Smithee, mailed
9063+ * to maintainer on pulped trees.
9064+ *
9065+ * This program is free software; you can redistribute it and/or modify it
9066+ * under the terms of the GNU General Public License as published by the
9067+ * Free Software Foundation; either version 2, or (at your option) any
9068+ * later version.
9069+ *
9070+ */
9071+
9072+typedef struct {
9073+ __u32 state[8];
9074+ __u32 count[2];
9075+ __u8 buf[128];
9076+} sha256_ctx_t;
9077+
9078+static __inline__ __u32
9079+generic_rotr32(const __u32 x, const unsigned bits)
9080+{
9081+ const unsigned n = bits % 32;
9082+ return (x >> n) | (x << (32 - n));
9083+}
9084+
9085+#define Ch(x,y,z) ((x & y) ^ (~x & z))
9086+#define Maj(x,y,z) ((x & y) ^ ( x & z) ^ (y & z))
9087+#define RORu32(x,y) generic_rotr32(x, y)
9088+#define e0(x) (RORu32(x, 2) ^ RORu32(x,13) ^ RORu32(x,22))
9089+#define e1(x) (RORu32(x, 6) ^ RORu32(x,11) ^ RORu32(x,25))
9090+#define s0(x) (RORu32(x, 7) ^ RORu32(x,18) ^ (x >> 3))
9091+#define s1(x) (RORu32(x,17) ^ RORu32(x,19) ^ (x >> 10))
9092+
9093+#define H0 0x6a09e667
9094+#define H1 0xbb67ae85
9095+#define H2 0x3c6ef372
9096+#define H3 0xa54ff53a
9097+#define H4 0x510e527f
9098+#define H5 0x9b05688c
9099+#define H6 0x1f83d9ab
9100+#define H7 0x5be0cd19
9101+
9102+const static __u32 sha256_K[64] = {
9103+ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
9104+ 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
9105+ 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
9106+ 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
9107+ 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
9108+ 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
9109+ 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
9110+ 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
9111+ 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
9112+ 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
9113+ 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
9114+ 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
9115+ 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
9116+ 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
9117+ 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
9118+ 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
9119+};
9120+
9121+#define LOAD_OP(I)\
9122+ {\
9123+ t1 = input[(4*I) ] & 0xff; t1<<=8;\
9124+ t1 |= input[(4*I)+1] & 0xff; t1<<=8;\
9125+ t1 |= input[(4*I)+2] & 0xff; t1<<=8;\
9126+ t1 |= input[(4*I)+3] & 0xff;\
9127+ W[I] = t1;\
9128+ }
9129+
9130+#define BLEND_OP(I) W[I] = s1(W[I-2]) + W[I-7] + s0(W[I-15]) + W[I-16];
9131+
9132+static void
9133+SHA256Transform(__u32 * state, const __u8 * input)
9134+{
9135+ __u32 a, b, c, d, e, f, g, h, t1, t2;
9136+ __u32 W[64];
9137+
9138+ int i;
9139+
9140+ /* load the input */
9141+ LOAD_OP(0);
9142+ LOAD_OP(1);
9143+ LOAD_OP(2);
9144+ LOAD_OP(3);
9145+ LOAD_OP(4);
9146+ LOAD_OP(5);
9147+ LOAD_OP(6);
9148+ LOAD_OP(7);
9149+ LOAD_OP(8);
9150+ LOAD_OP(9);
9151+ LOAD_OP(10);
9152+ LOAD_OP(11);
9153+ LOAD_OP(12);
9154+ LOAD_OP(13);
9155+ LOAD_OP(14);
9156+ LOAD_OP(15);
9157+
9158+ /* now blend */
9159+ for (i = 16; i < 64; i += 8) {
9160+ BLEND_OP(i);
9161+ BLEND_OP(i + 1);
9162+ BLEND_OP(i + 2);
9163+ BLEND_OP(i + 3);
9164+ BLEND_OP(i + 4);
9165+ BLEND_OP(i + 5);
9166+ BLEND_OP(i + 6);
9167+ BLEND_OP(i + 7);
9168+ }
9169+
9170+ /* load the state into our registers */
9171+ a = state[0];
9172+ b = state[1];
9173+ c = state[2];
9174+ d = state[3];
9175+ e = state[4];
9176+ f = state[5];
9177+ g = state[6];
9178+ h = state[7];
9179+
9180+ /* now iterate */
9181+ for (i = 0; i < 64; i += 8) {
9182+ t1 = h + e1(e) + Ch(e, f, g) + sha256_K[i] + W[i];
9183+ t2 = e0(a) + Maj(a, b, c);
9184+ d += t1;
9185+ h = t1 + t2;
9186+ t1 = g + e1(d) + Ch(d, e, f) + sha256_K[i + 1] + W[i + 1];
9187+ t2 = e0(h) + Maj(h, a, b);
9188+ c += t1;
9189+ g = t1 + t2;
9190+ t1 = f + e1(c) + Ch(c, d, e) + sha256_K[i + 2] + W[i + 2];
9191+ t2 = e0(g) + Maj(g, h, a);
9192+ b += t1;
9193+ f = t1 + t2;
9194+ t1 = e + e1(b) + Ch(b, c, d) + sha256_K[i + 3] + W[i + 3];
9195+ t2 = e0(f) + Maj(f, g, h);
9196+ a += t1;
9197+ e = t1 + t2;
9198+ t1 = d + e1(a) + Ch(a, b, c) + sha256_K[i + 4] + W[i + 4];
9199+ t2 = e0(e) + Maj(e, f, g);
9200+ h += t1;
9201+ d = t1 + t2;
9202+ t1 = c + e1(h) + Ch(h, a, b) + sha256_K[i + 5] + W[i + 5];
9203+ t2 = e0(d) + Maj(d, e, f);
9204+ g += t1;
9205+ c = t1 + t2;
9206+ t1 = b + e1(g) + Ch(g, h, a) + sha256_K[i + 6] + W[i + 6];
9207+ t2 = e0(c) + Maj(c, d, e);
9208+ f += t1;
9209+ b = t1 + t2;
9210+ t1 = a + e1(f) + Ch(f, g, h) + sha256_K[i + 7] + W[i + 7];
9211+ t2 = e0(b) + Maj(b, c, d);
9212+ e += t1;
9213+ a = t1 + t2;
9214+ }
9215+
9216+ state[0] += a;
9217+ state[1] += b;
9218+ state[2] += c;
9219+ state[3] += d;
9220+ state[4] += e;
9221+ state[5] += f;
9222+ state[6] += g;
9223+ state[7] += h;
9224+
9225+ /* clear any sensitive info... */
9226+ a = b = c = d = e = f = g = h = t1 = t2 = 0;
9227+ memset(W, 0, 64 * sizeof (__u32));
9228+}
9229+
9230+static void
9231+SHA256Init(sha256_ctx_t * C)
9232+{
9233+ C->state[0] = H0;
9234+ C->state[1] = H1;
9235+ C->state[2] = H2;
9236+ C->state[3] = H3;
9237+ C->state[4] = H4;
9238+ C->state[5] = H5;
9239+ C->state[6] = H6;
9240+ C->state[7] = H7;
9241+ C->count[0] = C->count[1] = 0;
9242+ memset(C->buf, 0, 128);
9243+}
9244+
9245+static void
9246+SHA256Update(sha256_ctx_t * C, const __u8 * input, __u32 inputLen)
9247+{
9248+ __u32 i, index, partLen;
9249+
9250+ /* Compute number of bytes mod 128 */
9251+ index = (__u32) ((C->count[0] >> 3) & 0x3f);
9252+
9253+ /* Update number of bits */
9254+ if ((C->count[0] += (inputLen << 3)) < (inputLen << 3)) {
9255+ C->count[1]++;
9256+ C->count[1] += (inputLen >> 29);
9257+ }
9258+
9259+ partLen = 64 - index;
9260+
9261+ /* Transform as many times as possible. */
9262+ if (inputLen >= partLen) {
9263+ memcpy(&C->buf[index], input, partLen);
9264+ SHA256Transform(C->state, C->buf);
9265+
9266+ for (i = partLen; i + 63 < inputLen; i += 64)
9267+ SHA256Transform(C->state, &input[i]);
9268+ index = 0;
9269+ } else {
9270+ i = 0;
9271+ }
9272+
9273+ /* Buffer remaining input */
9274+ memcpy(&C->buf[index], &input[i], inputLen - i);
9275+}
9276+
9277+static void
9278+SHA256Final(sha256_ctx_t * C, __u8 * digest)
9279+{
9280+ const static __u8 padding[64] = { 0x80, };
9281+ __u8 bits[8];
9282+ __u32 t, index, padLen;
9283+ int i, j;
9284+
9285+ /* Save number of bits */
9286+ t = C->count[0];
9287+ bits[7] = t;
9288+ t >>= 8;
9289+ bits[6] = t;
9290+ t >>= 8;
9291+ bits[5] = t;
9292+ t >>= 8;
9293+ bits[4] = t;
9294+ t = C->count[1];
9295+ bits[3] = t;
9296+ t >>= 8;
9297+ bits[2] = t;
9298+ t >>= 8;
9299+ bits[1] = t;
9300+ t >>= 8;
9301+ bits[0] = t;
9302+
9303+ /* Pad out to 56 mod 64. */
9304+ index = (C->count[0] >> 3) & 0x3f;
9305+ padLen = (index < 56) ? (56 - index) : ((64 + 56) - index);
9306+ SHA256Update(C, padding, padLen);
9307+
9308+ /* Append length (before padding) */
9309+ SHA256Update(C, bits, sizeof (bits));
9310+
9311+ /* Store state in digest */
9312+ for (i = j = 0; i < 8; i++, j += 4) {
9313+ t = C->state[i];
9314+ digest[j + 3] = t;
9315+ t >>= 8;
9316+ digest[j + 2] = t;
9317+ t >>= 8;
9318+ digest[j + 1] = t;
9319+ t >>= 8;
9320+ digest[j] = t;
9321+ }
9322+
9323+ /* Zeroize sensitive information. */
9324+ memset(C, 0, sizeof (sha256_ctx_t));
9325+}
9326+
9327+int
9328+chkpw(struct gr_arg *entry, unsigned char *salt, unsigned char *sum)
9329+{
9330+ sha256_ctx_t context;
9331+ unsigned char temp_sum[GR_SHA_LEN];
9332+ volatile int retval = 0;
9333+ volatile int dummy = 0;
9334+ unsigned int i;
9335+
9336+ SHA256Init(&context);
9337+ SHA256Update(&context, salt, GR_SALT_LEN);
9338+ SHA256Update(&context, entry->pw, strlen(entry->pw));
9339+ SHA256Final(&context, temp_sum);
9340+
9341+ memset(entry->pw, 0, GR_PW_LEN);
9342+
9343+ for (i = 0; i < GR_SHA_LEN; i++)
9344+ if (sum[i] != temp_sum[i])
9345+ retval = 1;
9346+ else
9347+ dummy = 1; // waste a cycle
9348+
9349+ return retval;
9350+}
9351diff -urN linux-2.4.21/grsecurity/obsd_rand.c linux-2.4.21/grsecurity/obsd_rand.c
9352--- linux-2.4.21/grsecurity/obsd_rand.c 1969-12-31 19:00:00.000000000 -0500
9353+++ linux-2.4.21/grsecurity/obsd_rand.c 2003-06-23 11:49:16.000000000 -0400
9354@@ -0,0 +1,299 @@
9355+
9356+/*
9357+ * Copyright (c) 1996, 1997, 2000-2002 Michael Shalayeff.
9358+ *
9359+ * Version 1.89, last modified 19-Sep-99
9360+ *
9361+ * Copyright Theodore Ts'o, 1994, 1995, 1996, 1997, 1998, 1999.
9362+ * All rights reserved.
9363+ *
9364+ * Copyright 1998 Niels Provos <provos@citi.umich.edu>
9365+ * All rights reserved.
9366+ * Theo de Raadt <deraadt@openbsd.org> came up with the idea of using
9367+ * such a mathematical system to generate more random (yet non-repeating)
9368+ * ids to solve the resolver/named problem. But Niels designed the
9369+ * actual system based on the constraints.
9370+ *
9371+ * Redistribution and use in source and binary forms, with or without
9372+ * modification, are permitted provided that the following conditions
9373+ * are met:
9374+ * 1. Redistributions of source code must retain the above copyright
9375+ * notice, this list of conditions and the following disclaimer,
9376+ * 2. Redistributions in binary form must reproduce the above copyright
9377+ * notice, this list of conditions and the following disclaimer in the
9378+ * documentation and/or other materials provided with the distribution.
9379+ * 3. All advertising materials mentioning features or use of this software
9380+ * must display the following acknowledgement:
9381+ * This product includes software developed by Niels Provos.
9382+ * 4. The name of the author may not be used to endorse or promote products
9383+ * derived from this software without specific prior written permission.
9384+ *
9385+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
9386+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
9387+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
9388+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
9389+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
9390+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
9391+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
9392+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
9393+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
9394+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
9395+ */
9396+
9397+#include <linux/kernel.h>
9398+#include <linux/sched.h>
9399+#include <linux/timer.h>
9400+#include <linux/smp_lock.h>
9401+#include <linux/random.h>
9402+
9403+#define RU_OUT 180
9404+#define RU_MAX 30000
9405+#define RU_GEN 2
9406+#define RU_N 32749
9407+#define RU_AGEN 7
9408+#define RU_M 31104
9409+#define PFAC_N 3
9410+const static __u16 pfacts[PFAC_N] = { 2, 3, 2729 };
9411+
9412+static __u16 ru_x;
9413+static __u16 ru_seed, ru_seed2;
9414+static __u16 ru_a, ru_b;
9415+static __u16 ru_g;
9416+static __u16 ru_counter = 0;
9417+static __u16 ru_msb = 0;
9418+static unsigned long ru_reseed = 0;
9419+static __u32 tmp;
9420+
9421+static struct arc4_stream {
9422+ __u8 s[256];
9423+ unsigned int cnt;
9424+ __u8 i;
9425+ __u8 j;
9426+} arc4random_state[NR_CPUS];
9427+
9428+#define TCP_RNDISS_ROUNDS 15
9429+#define TCP_RNDISS_OUT 7200
9430+#define TCP_RNDISS_MAX 30000
9431+
9432+static __u8 tcp_rndiss_sbox[NR_CPUS][128];
9433+static __u16 tcp_rndiss_msb[NR_CPUS];
9434+static __u16 tcp_rndiss_cnt[NR_CPUS];
9435+static unsigned long tcp_rndiss_reseed[NR_CPUS];
9436+
9437+static unsigned long arc4random_reseed[NR_CPUS];
9438+
9439+static int arc4random_initialized[NR_CPUS];
9440+
9441+static __u16 pmod(__u16, __u16, __u16);
9442+static void ip_initid(void);
9443+__u16 ip_randomid(void);
9444+
9445+static __inline__ __u8 arc4_getbyte(void);
9446+static __inline__ void arc4_stir(void);
9447+static void arc4maybeinit(void);
9448+__u32 arc4random(void);
9449+
9450+static __inline__ __u8
9451+arc4_getbyte(void)
9452+{
9453+ register __u8 si, sj;
9454+ int cpu = smp_processor_id();
9455+
9456+ arc4random_state[cpu].cnt++;
9457+ arc4random_state[cpu].i++;
9458+ si = arc4random_state[cpu].s[arc4random_state[cpu].i];
9459+ arc4random_state[cpu].j += si;
9460+ sj = arc4random_state[cpu].s[arc4random_state[cpu].j];
9461+ arc4random_state[cpu].s[arc4random_state[cpu].i] = sj;
9462+ arc4random_state[cpu].s[arc4random_state[cpu].j] = si;
9463+ return arc4random_state[cpu].s[(si + sj) & 0xff];
9464+}
9465+
9466+static __inline__ void
9467+arc4_stir(void)
9468+{
9469+ __u8 buf[256];
9470+ register __u8 si;
9471+ register int n;
9472+ int len = sizeof (buf);
9473+ int cpu = smp_processor_id();
9474+ unsigned long flags;
9475+
9476+ do_gettimeofday((struct timeval *) buf);
9477+ get_random_bytes(buf + sizeof (struct timeval),
9478+ len - sizeof (struct timeval));
9479+
9480+ save_flags(flags);
9481+ cli();
9482+
9483+ arc4random_state[cpu].i--;
9484+ for (n = 0; n < 256; n++) {
9485+ arc4random_state[cpu].i++;
9486+ si = arc4random_state[cpu].s[arc4random_state[cpu].i];
9487+ arc4random_state[cpu].j += si + buf[n % len];
9488+ arc4random_state[cpu].s[arc4random_state[cpu].i] =
9489+ arc4random_state[cpu].s[arc4random_state[cpu].j];
9490+ arc4random_state[cpu].s[arc4random_state[cpu].j] = si;
9491+ }
9492+ arc4random_state[cpu].j = arc4random_state[cpu].i;
9493+ arc4random_state[cpu].cnt = 0;
9494+
9495+ restore_flags(flags);
9496+
9497+ for (n = 0; n < 256 * 4; n++)
9498+ arc4_getbyte();
9499+
9500+ return;
9501+}
9502+
9503+void
9504+arc4_init(void)
9505+{
9506+ int i, n;
9507+
9508+ for (n = 0; n < NR_CPUS; n++) {
9509+ for (i = 0; i < 256; i++)
9510+ arc4random_state[n].s[i] = i;
9511+ arc4random_initialized[n] = 0;
9512+ }
9513+}
9514+
9515+static void
9516+arc4maybeinit(void)
9517+{
9518+ int cpu = smp_processor_id();
9519+
9520+ if (arc4random_initialized[cpu] &&
9521+ time_after(xtime.tv_sec, arc4random_reseed[cpu]))
9522+ arc4random_initialized[cpu] = 0;
9523+
9524+ if (!arc4random_initialized[cpu]) {
9525+ arc4random_initialized[cpu]++;
9526+ arc4_stir();
9527+ arc4random_reseed[cpu] = xtime.tv_sec + (10 * 60);
9528+ }
9529+}
9530+
9531+__u32
9532+arc4random(void)
9533+{
9534+ arc4maybeinit();
9535+ return ((arc4_getbyte() << 24) | (arc4_getbyte() << 16)
9536+ | (arc4_getbyte() << 8) | arc4_getbyte());
9537+}
9538+
9539+static __u16
9540+pmod(__u16 gen, __u16 exp, __u16 mod)
9541+{
9542+ __u16 s, t, u;
9543+
9544+ s = 1;
9545+ t = gen;
9546+ u = exp;
9547+
9548+ while (u) {
9549+ if (u & 1)
9550+ s = (s * t) % mod;
9551+ u >>= 1;
9552+ t = (t * t) % mod;
9553+ }
9554+ return (s);
9555+}
9556+
9557+static void
9558+ip_initid(void)
9559+{
9560+ __u16 j, i;
9561+ int noprime = 1;
9562+
9563+ ru_x = ((tmp = arc4random()) & 0xFFFF) % RU_M;
9564+
9565+ ru_seed = (tmp >> 16) & 0x7FFF;
9566+ ru_seed2 = arc4random() & 0x7FFF;
9567+
9568+ ru_b = ((tmp = arc4random()) & 0xfffe) | 1;
9569+ ru_a = pmod(RU_AGEN, (tmp >> 16) & 0xfffe, RU_M);
9570+ while (ru_b % 3 == 0)
9571+ ru_b += 2;
9572+
9573+ j = (tmp = arc4random()) % RU_N;
9574+ tmp = tmp >> 16;
9575+
9576+ while (noprime) {
9577+ for (i = 0; i < PFAC_N; i++)
9578+ if (j % pfacts[i] == 0)
9579+ break;
9580+
9581+ if (i >= PFAC_N)
9582+ noprime = 0;
9583+ else
9584+ j = (j + 1) % RU_N;
9585+ }
9586+
9587+ ru_g = pmod(RU_GEN, j, RU_N);
9588+ ru_counter = 0;
9589+
9590+ ru_reseed = xtime.tv_sec + RU_OUT;
9591+ ru_msb = ru_msb == 0x8000 ? 0 : 0x8000;
9592+}
9593+
9594+__u16
9595+ip_randomid(void)
9596+{
9597+ int i, n;
9598+
9599+ if (ru_counter >= RU_MAX || time_after(xtime.tv_sec, ru_reseed))
9600+ ip_initid();
9601+
9602+ if (!tmp)
9603+ tmp = arc4random();
9604+
9605+ n = tmp & 0x3;
9606+ tmp = tmp >> 2;
9607+ if (ru_counter + n >= RU_MAX)
9608+ ip_initid();
9609+ for (i = 0; i <= n; i++)
9610+ ru_x = (ru_a * ru_x + ru_b) % RU_M;
9611+ ru_counter += i;
9612+
9613+ return ((ru_seed ^ pmod(ru_g, ru_seed2 ^ ru_x, RU_N)) | ru_msb);
9614+}
9615+
9616+__u16
9617+tcp_rndiss_encrypt(__u16 val)
9618+{
9619+ __u16 sum = 0, i;
9620+ int cpu = smp_processor_id();
9621+
9622+ for (i = 0; i < TCP_RNDISS_ROUNDS; i++) {
9623+ sum += 0x79b9;
9624+ val ^= ((__u16) tcp_rndiss_sbox[cpu][(val ^ sum) ^ 0x7f]) << 7;
9625+ val = ((val & 0xff) << 7) | (val >> 8);
9626+ }
9627+
9628+ return val;
9629+}
9630+
9631+static void
9632+tcp_rndiss_init(void)
9633+{
9634+ int cpu = smp_processor_id();
9635+
9636+ get_random_bytes(tcp_rndiss_sbox[cpu], sizeof (tcp_rndiss_sbox));
9637+ tcp_rndiss_reseed[cpu] = xtime.tv_sec + TCP_RNDISS_OUT;
9638+ tcp_rndiss_msb[cpu] = tcp_rndiss_msb[cpu] == 0x8000 ? 0 : 0x8000;
9639+ tcp_rndiss_cnt[cpu] = 0;
9640+}
9641+
9642+__u32
9643+ip_randomisn(void)
9644+{
9645+ int cpu = smp_processor_id();
9646+
9647+ if (tcp_rndiss_cnt[cpu] >= TCP_RNDISS_MAX ||
9648+ time_after(xtime.tv_sec, tcp_rndiss_reseed[cpu]))
9649+ tcp_rndiss_init();
9650+
9651+ return (((tcp_rndiss_encrypt(tcp_rndiss_cnt[cpu]++) |
9652+ tcp_rndiss_msb[cpu]) << 16) | (arc4random() & 0x7fff));
9653+}
9654diff -urN linux-2.4.21/include/linux/fs.h linux-2.4.21/include/linux/fs.h
9655--- linux-2.4.21/include/linux/fs.h 2003-06-23 11:41:32.000000000 -0400
9656+++ linux-2.4.21/include/linux/fs.h 2003-06-23 11:49:17.000000000 -0400
9657@@ -1070,7 +1070,7 @@
9658
9659 asmlinkage long sys_open(const char *, int, int);
9660 asmlinkage long sys_close(unsigned int); /* yes, it's really unsigned */
9661-extern int do_truncate(struct dentry *, loff_t start);
9662+extern int do_truncate(struct dentry *, loff_t start, struct vfsmount *);
9663
9664 extern struct file *filp_open(const char *, int, int);
9665 extern struct file * dentry_open(struct dentry *, struct vfsmount *, int);
9666diff -urN linux-2.4.21/include/linux/gracl.h linux-2.4.21/include/linux/gracl.h
9667--- linux-2.4.21/include/linux/gracl.h 1969-12-31 19:00:00.000000000 -0500
9668+++ linux-2.4.21/include/linux/gracl.h 2003-06-23 11:49:17.000000000 -0400
9669@@ -0,0 +1,209 @@
9670+#ifndef GR_ACL_H
9671+#define GR_ACL_H
9672+#endif
9673+#include <linux/grdefs.h>
9674+#include <linux/resource.h>
9675+
9676+#include <asm/resource.h>
9677+
9678+/* * * * * * * * * * * * * * * * * * * * *
9679+ * grsecurity ACL System
9680+ * Main header file
9681+ * Purpose: define most gracl data structures
9682+ * * * * * * * * * * * * * * * * * * * * */
9683+
9684+/* Major status information */
9685+
9686+#define GR_VERSION "grsecurity 2.0"
9687+
9688+enum {
9689+
9690+ SHUTDOWN = 0,
9691+ ENABLE = 1,
9692+ SPROLE = 2,
9693+ RELOAD = 3,
9694+ SEGVMOD = 4
9695+};
9696+
9697+/* Password setup definitions
9698+ * kernel/grhash.c */
9699+enum {
9700+ GR_PW_LEN = 128,
9701+ GR_SALT_LEN = 16,
9702+ GR_SHA_LEN = 32,
9703+};
9704+
9705+enum {
9706+ GR_SPROLE_LEN = 64,
9707+};
9708+
9709+/* Begin Data Structures */
9710+
9711+struct sprole_pw {
9712+ unsigned char *rolename;
9713+ unsigned char salt[GR_SALT_LEN];
9714+ unsigned char sum[GR_SHA_LEN]; /* 256-bit SHA hash of the password */
9715+};
9716+
9717+struct name_entry {
9718+ ino_t inode;
9719+ kdev_t device;
9720+ char *name;
9721+};
9722+
9723+struct acl_role_db {
9724+ struct acl_role_label **r_hash;
9725+ __u32 r_size;
9726+};
9727+
9728+struct name_db {
9729+ struct name_entry **n_hash;
9730+ __u32 n_size;
9731+};
9732+
9733+struct crash_uid {
9734+ uid_t uid;
9735+ unsigned long expires;
9736+};
9737+
9738+/* Userspace Grsecurity ACL data structures */
9739+struct acl_subject_label {
9740+ char *filename;
9741+ ino_t inode;
9742+ kdev_t device;
9743+ __u32 mode;
9744+ __u32 cap_raise;
9745+ __u32 cap_lower;
9746+
9747+ struct rlimit res[RLIM_NLIMITS + 1];
9748+ __u16 resmask;
9749+
9750+ __u32 ip_proto[8];
9751+ __u32 ip_type;
9752+ struct acl_ip_label **ips;
9753+ __u32 ip_num;
9754+
9755+ __u32 crashes;
9756+ unsigned long expires;
9757+
9758+ struct acl_subject_label *parent_subject;
9759+ struct acl_object_label *proc_object;
9760+ struct acl_ip_label *ip_object;
9761+ struct acl_subject_label *prev;
9762+ struct acl_subject_label *next;
9763+
9764+ struct acl_object_label **obj_hash;
9765+ __u32 obj_hash_size;
9766+};
9767+
9768+struct role_allowed_ip {
9769+ __u32 addr;
9770+ __u32 netmask;
9771+
9772+ struct role_allowed_ip *prev;
9773+ struct role_allowed_ip *next;
9774+};
9775+
9776+struct role_transition {
9777+ char *rolename;
9778+
9779+ struct role_transition *prev;
9780+ struct role_transition *next;
9781+};
9782+
9783+struct acl_role_label {
9784+ char *rolename;
9785+ uid_t uidgid;
9786+ __u16 roletype;
9787+
9788+ __u16 auth_attempts;
9789+ unsigned long expires;
9790+
9791+ struct acl_subject_label *root_label;
9792+ struct acl_subject_label *proc_subject;
9793+
9794+ struct acl_role_label *prev;
9795+ struct acl_role_label *next;
9796+
9797+ struct role_transition *transitions;
9798+ struct role_allowed_ip *allowed_ips;
9799+ struct acl_subject_label **subj_hash;
9800+ __u32 subj_hash_size;
9801+};
9802+
9803+struct user_acl_role_db {
9804+ struct acl_role_label **r_table;
9805+ __u32 r_entries; /* number of entries in table */
9806+ __u32 s_entries; /* total number of subject acls */
9807+ __u32 i_entries; /* total number of ip acls */
9808+ __u32 o_entries; /* Total number of object acls */
9809+ __u32 a_entries; /* total number of allowed ips */
9810+ __u32 t_entries; /* total number of transitions */
9811+};
9812+
9813+struct acl_object_label {
9814+ char *filename;
9815+ ino_t inode;
9816+ kdev_t device;
9817+ __u32 mode;
9818+
9819+ struct acl_subject_label *nested;
9820+
9821+ /* next two structures not used */
9822+
9823+ struct acl_object_label *prev;
9824+ struct acl_object_label *next;
9825+};
9826+
9827+struct acl_ip_label {
9828+ __u32 addr;
9829+ __u32 netmask;
9830+ __u16 low, high;
9831+ __u8 mode;
9832+ __u32 type;
9833+ __u32 proto[8];
9834+
9835+ /* next two structures not used */
9836+
9837+ struct acl_ip_label *prev;
9838+ struct acl_ip_label *next;
9839+};
9840+
9841+struct gr_arg {
9842+ struct user_acl_role_db role_db;
9843+ unsigned char pw[GR_PW_LEN];
9844+ unsigned char salt[GR_SALT_LEN];
9845+ unsigned char sum[GR_SHA_LEN];
9846+ unsigned char sp_role[GR_SPROLE_LEN];
9847+ struct sprole_pw *sprole_pws;
9848+ __u16 num_sprole_pws;
9849+ kdev_t segv_device;
9850+ ino_t segv_inode;
9851+ uid_t segv_uid;
9852+ __u16 mode;
9853+};
9854+
9855+/* End Data Structures Section */
9856+
9857+/* Hash functions generated by empirical testing by Brad Spengler
9858+ Makes good use of the low bits of the inode. Generally 0-1 times
9859+ in loop for successful match. 0-3 for unsuccessful match.
9860+ Shift/add algorithm with modulus of table size and an XOR*/
9861+
9862+static __inline__ unsigned long
9863+rhash(const uid_t uid, const __u16 type, const unsigned long sz)
9864+{
9865+ return (((uid << type) + (uid ^ type)) % sz);
9866+}
9867+
9868+static __inline__ unsigned long
9869+fhash(const ino_t ino, const kdev_t dev, const unsigned long sz)
9870+{
9871+ return (((ino + dev) ^ ((ino << 13) + (ino << 23) + (dev << 9))) % sz);
9872+}
9873+
9874+static __inline__ unsigned long
9875+nhash(const char *name, const unsigned long sz)
9876+{
9877+ return full_name_hash(name, strlen(name)) % sz;
9878+}
9879diff -urN linux-2.4.21/include/linux/gralloc.h linux-2.4.21/include/linux/gralloc.h
9880--- linux-2.4.21/include/linux/gralloc.h 1969-12-31 19:00:00.000000000 -0500
9881+++ linux-2.4.21/include/linux/gralloc.h 2003-06-23 11:49:17.000000000 -0400
9882@@ -0,0 +1,8 @@
9883+#ifndef __GRALLOC_H
9884+#define __GRALLOC_H
9885+
9886+void acl_free_all(void);
9887+int acl_alloc_stack_init(unsigned long size);
9888+void *acl_alloc(unsigned long len);
9889+
9890+#endif
9891diff -urN linux-2.4.21/include/linux/grdefs.h linux-2.4.21/include/linux/grdefs.h
9892--- linux-2.4.21/include/linux/grdefs.h 1969-12-31 19:00:00.000000000 -0500
9893+++ linux-2.4.21/include/linux/grdefs.h 2003-06-23 11:49:17.000000000 -0400
9894@@ -0,0 +1,96 @@
9895+#ifndef GRDEFS_H
9896+#define GRDEFS_H
9897+
9898+/* Begin grsecurity status declarations */
9899+
9900+enum {
9901+ GR_READY = 0x01,
9902+ GR_STATUS_INIT = 0x00 // disabled state
9903+};
9904+
9905+/* Begin ACL declarations */
9906+
9907+/* Role flags */
9908+
9909+enum {
9910+ GR_ROLE_USER = 0x0001,
9911+ GR_ROLE_GROUP = 0x0002,
9912+ GR_ROLE_DEFAULT = 0x0004,
9913+ GR_ROLE_SPECIAL = 0x0008,
9914+ GR_ROLE_AUTH = 0x0010,
9915+ GR_ROLE_NOPW = 0x0020,
9916+ GR_ROLE_GOD = 0x0040
9917+};
9918+
9919+/* ACL Subject and Object mode flags */
9920+enum {
9921+ GR_DELETED = 0x00000080
9922+};
9923+
9924+/* ACL Object-only mode flags */
9925+enum {
9926+ GR_READ = 0x00000001,
9927+ GR_APPEND = 0x00000002,
9928+ GR_WRITE = 0x00000004,
9929+ GR_EXEC = 0x00000008,
9930+ GR_FIND = 0x00000010,
9931+ GR_INHERIT = 0x00000040,
9932+ GR_PTRACERD = 0x00000100,
9933+ GR_SETID = 0x00000200,
9934+ GR_CREATE = 0x00000400,
9935+ GR_DELETE = 0x00000800,
9936+ GR_AUDIT_READ = 0x00001000,
9937+ GR_AUDIT_APPEND = 0x00002000,
9938+ GR_AUDIT_WRITE = 0x0004000,
9939+ GR_AUDIT_EXEC = 0x00008000,
9940+ GR_AUDIT_FIND = 0x00010000,
9941+ GR_AUDIT_INHERIT = 0x00020000,
9942+ GR_AUDIT_SETID = 0x00400000,
9943+ GR_AUDIT_CREATE = 0x00800000,
9944+ GR_AUDIT_DELETE = 0x01000000,
9945+ GR_SUPPRESS = 0x02000000,
9946+ GR_NOLEARN = 0x04000000
9947+};
9948+
9949+#define GR_AUDITS (GR_AUDIT_READ | GR_AUDIT_WRITE | GR_AUDIT_APPEND | GR_AUDIT_EXEC | \
9950+ GR_AUDIT_FIND | GR_AUDIT_INHERIT | GR_AUDIT_SETID | \
9951+ GR_AUDIT_CREATE | GR_AUDIT_DELETE)
9952+
9953+/* ACL subject-only mode flags */
9954+enum {
9955+ GR_KILL = 0x00000001,
9956+ GR_VIEW = 0x00000002,
9957+ GR_PROTECTED = 0x00000100,
9958+ GR_LEARN = 0x00000200,
9959+ GR_OVERRIDE = 0x00000400,
9960+ /* just a placeholder, this mode is only used in userspace */
9961+ GR_DUMMY = 0x00000800,
9962+
9963+ GR_PROTSHM = 0x00040000,
9964+ GR_KILLPROC = 0x00080000,
9965+ GR_KILLIPPROC = 0x00100000,
9966+ /* just a placeholder, this mode is only used in userspace */
9967+ GR_NOTROJAN = 0x00200000,
9968+ GR_PROTPROCFD = 0x00400000,
9969+ GR_PROCACCT = 0x00800000
9970+};
9971+
9972+#define GR_CRASH_RES 11
9973+#define GR_UIDTABLE_MAX 500
9974+
9975+/* begin resource learning section */
9976+enum {
9977+ GR_RLIM_CPU_BUMP = 60,
9978+ GR_RLIM_FSIZE_BUMP = 50000,
9979+ GR_RLIM_DATA_BUMP = 10000,
9980+ GR_RLIM_STACK_BUMP = 1000,
9981+ GR_RLIM_CORE_BUMP = 10000,
9982+ GR_RLIM_RSS_BUMP = 500000,
9983+ GR_RLIM_NPROC_BUMP = 1,
9984+ GR_RLIM_NOFILE_BUMP = 5,
9985+ GR_RLIM_MEMLOCK_BUMP = 50000,
9986+ GR_RLIM_AS_BUMP = 500000,
9987+ GR_RLIM_LOCKS_BUMP = 2
9988+};
9989+
9990+#endif
9991diff -urN linux-2.4.21/include/linux/grinternal.h linux-2.4.21/include/linux/grinternal.h
9992--- linux-2.4.21/include/linux/grinternal.h 1969-12-31 19:00:00.000000000 -0500
9993+++ linux-2.4.21/include/linux/grinternal.h 2003-06-23 11:49:17.000000000 -0400
9994@@ -0,0 +1,177 @@
9995+#ifndef __GRINTERNAL_H
9996+#define __GRINTERNAL_H
9997+
9998+#ifdef CONFIG_GRKERNSEC
9999+
10000+#include <linux/grdefs.h>
10001+#include <linux/grmsg.h>
10002+
10003+extern void gr_add_learn_entry(const char *fmt, ...);
10004+extern __u32 gr_search_file(const struct dentry *dentry, const __u32 mode,
10005+ const struct vfsmount *mnt);
10006+extern __u32 gr_check_create(const struct dentry *new_dentry,
10007+ const struct dentry *parent,
10008+ const struct vfsmount *mnt, const __u32 mode);
10009+extern int gr_check_protected_task(const struct task_struct *task);
10010+extern __inline__ __u32 to_gr_audit(const __u32 reqmode);
10011+extern int gr_handle_rename(struct inode *old_dir, struct inode *new_dir,
10012+ struct dentry *old_dentry,
10013+ struct dentry *new_dentry,
10014+ struct vfsmount *mnt, const __u8 replace);
10015+extern int gr_set_acls(const int type);
10016+
10017+extern void gr_handle_alertkill(void);
10018+extern char *gr_to_filename(const struct dentry *dentry,
10019+ const struct vfsmount *mnt);
10020+
10021+extern int grsec_enable_link;
10022+extern int grsec_enable_fifo;
10023+extern int grsec_enable_execve;
10024+extern int grsec_enable_forkbomb;
10025+extern int grsec_forkbomb_gid;
10026+extern int grsec_forkbomb_sec;
10027+extern int grsec_forkbomb_max;
10028+extern int grsec_enable_execlog;
10029+extern int grsec_enable_signal;
10030+extern int grsec_enable_forkfail;
10031+extern int grsec_enable_time;
10032+extern int grsec_enable_chroot_shmat;
10033+extern int grsec_enable_chroot_findtask;
10034+extern int grsec_enable_chroot_mount;
10035+extern int grsec_enable_chroot_double;
10036+extern int grsec_enable_chroot_pivot;
10037+extern int grsec_enable_chroot_chdir;
10038+extern int grsec_enable_chroot_chmod;
10039+extern int grsec_enable_chroot_mknod;
10040+extern int grsec_enable_chroot_fchdir;
10041+extern int grsec_enable_chroot_nice;
10042+extern int grsec_enable_chroot_execlog;
10043+extern int grsec_enable_chroot_caps;
10044+extern int grsec_enable_chroot_sysctl;
10045+extern int grsec_enable_chroot_unix;
10046+extern int grsec_enable_tpe;
10047+extern int grsec_tpe_gid;
10048+extern int grsec_enable_tpe_all;
10049+extern int grsec_enable_sidcaps;
10050+extern int grsec_enable_randpid;
10051+extern int grsec_enable_socket_all;
10052+extern int grsec_socket_all_gid;
10053+extern int grsec_enable_socket_client;
10054+extern int grsec_socket_client_gid;
10055+extern int grsec_enable_socket_server;
10056+extern int grsec_socket_server_gid;
10057+extern int grsec_audit_gid;
10058+extern int grsec_enable_group;
10059+extern int grsec_enable_audit_ipc;
10060+extern int grsec_enable_mount;
10061+extern int grsec_enable_chdir;
10062+extern int grsec_lock;
10063+
10064+extern struct task_struct *child_reaper;
10065+
10066+extern spinlock_t grsec_alert_lock;
10067+extern unsigned long grsec_alert_wtime;
10068+extern unsigned long grsec_alert_fyet;
10069+
10070+extern spinlock_t grsec_alertgood_lock;
10071+extern unsigned long grsec_alertgood_wtime;
10072+extern unsigned long grsec_alertgood_fyet;
10073+
10074+extern spinlock_t grsec_audit_lock;
10075+
10076+#define proc_is_chrooted(tsk_a) ((tsk_a->pid > 1) && \
10077+ ((tsk_a->fs->root->d_inode->i_dev != \
10078+ child_reaper->fs->root->d_inode->i_dev) || \
10079+ (tsk_a->fs->root->d_inode->i_ino != \
10080+ child_reaper->fs->root->d_inode->i_ino)))
10081+
10082+#define have_same_root(tsk_a,tsk_b) ((tsk_a->fs->root->d_inode->i_dev == \
10083+ tsk_b->fs->root->d_inode->i_dev) && \
10084+ (tsk_a->fs->root->d_inode->i_ino == \
10085+ tsk_b->fs->root->d_inode->i_ino))
10086+
10087+#define DEFAULTSECARGS current->comm, current->pid, current->uid, \
10088+ current->euid, current->gid, current->egid, \
10089+ current->p_pptr->comm, current->p_pptr->pid, \
10090+ current->p_pptr->uid, current->p_pptr->euid, \
10091+ current->p_pptr->gid, current->p_pptr->egid
10092+
10093+#define GR_CHROOT_CAPS ( \
10094+ CAP_TO_MASK(CAP_FOWNER) | \
10095+ CAP_TO_MASK(CAP_LINUX_IMMUTABLE) | CAP_TO_MASK(CAP_NET_ADMIN) | \
10096+ CAP_TO_MASK(CAP_SYS_MODULE) | CAP_TO_MASK(CAP_SYS_RAWIO) | \
10097+ CAP_TO_MASK(CAP_SYS_PACCT) | CAP_TO_MASK(CAP_SYS_ADMIN) | \
10098+ CAP_TO_MASK(CAP_SYS_BOOT) | CAP_TO_MASK(CAP_SYS_TIME) | \
10099+ CAP_TO_MASK(CAP_NET_RAW) | CAP_TO_MASK(CAP_SYS_TTY_CONFIG) | \
10100+ CAP_TO_MASK(CAP_IPC_OWNER))
10101+
10102+#define security_alert_good(normal_msg,args...) \
10103+({ \
10104+ spin_lock(&grsec_alertgood_lock); \
10105+ \
10106+ if (!grsec_alertgood_wtime || jiffies - grsec_alertgood_wtime > CONFIG_GRKERNSEC_FLOODTIME * HZ) { \
10107+ grsec_alertgood_wtime = jiffies; grsec_alertgood_fyet = 0; \
10108+ if (current->curr_ip) \
10109+ printk(KERN_ALERT "grsec: From %u.%u.%u.%u: " normal_msg "\n", NIPQUAD(current->curr_ip) , ## args); \
10110+ else \
10111+ printk(KERN_ALERT "grsec: " normal_msg "\n" , ## args); \
10112+ } else if((jiffies - grsec_alertgood_wtime < CONFIG_GRKERNSEC_FLOODTIME * HZ) && (grsec_alertgood_fyet < CONFIG_GRKERNSEC_FLOODBURST)) { \
10113+ grsec_alertgood_fyet++; \
10114+ if (current->curr_ip) \
10115+ printk(KERN_ALERT "grsec: From %u.%u.%u.%u: " normal_msg "\n", NIPQUAD(current->curr_ip) , ## args); \
10116+ else \
10117+ printk(KERN_ALERT "grsec: " normal_msg "\n" , ## args); \
10118+ } else if (grsec_alertgood_fyet == CONFIG_GRKERNSEC_FLOODBURST) { \
10119+ grsec_alertgood_wtime = jiffies; grsec_alertgood_fyet++; \
10120+ printk(KERN_ALERT "grsec: more alerts, logging disabled for " \
10121+ "%d seconds\n", CONFIG_GRKERNSEC_FLOODTIME); \
10122+ } \
10123+ \
10124+ spin_unlock(&grsec_alertgood_lock); \
10125+})
10126+
10127+#define security_alert(normal_msg,args...) \
10128+({ \
10129+ spin_lock(&grsec_alert_lock); \
10130+ \
10131+ if (!grsec_alert_wtime || jiffies - grsec_alert_wtime > CONFIG_GRKERNSEC_FLOODTIME * HZ) { \
10132+ grsec_alert_wtime = jiffies; grsec_alert_fyet = 0; \
10133+ if (current->curr_ip) \
10134+ printk(KERN_ALERT "grsec: From %u.%u.%u.%u: " normal_msg "\n", NIPQUAD(current->curr_ip) , ## args); \
10135+ else \
10136+ printk(KERN_ALERT "grsec: " normal_msg "\n" , ## args); \
10137+ } else if((jiffies - grsec_alert_wtime < CONFIG_GRKERNSEC_FLOODTIME * HZ) && (grsec_alert_fyet < CONFIG_GRKERNSEC_FLOODBURST)) { \
10138+ grsec_alert_fyet++; \
10139+ if (current->curr_ip) \
10140+ printk(KERN_ALERT "grsec: From %u.%u.%u.%u: " normal_msg "\n", NIPQUAD(current->curr_ip) , ## args); \
10141+ else \
10142+ printk(KERN_ALERT "grsec: " normal_msg "\n" , ## args); \
10143+ } else if (grsec_alert_fyet == CONFIG_GRKERNSEC_FLOODBURST) { \
10144+ grsec_alert_wtime = jiffies; grsec_alert_fyet++; \
10145+ printk(KERN_ALERT "grsec: more alerts, logging disabled for " \
10146+ "%d seconds\n", CONFIG_GRKERNSEC_FLOODTIME); \
10147+ } \
10148+ \
10149+ gr_handle_alertkill(); \
10150+ spin_unlock(&grsec_alert_lock); \
10151+})
10152+
10153+#define security_audit(normal_msg,args...) \
10154+({ \
10155+ spin_lock(&grsec_audit_lock); \
10156+ if (current->curr_ip) \
10157+ printk(KERN_INFO "grsec: From %u.%u.%u.%u: " normal_msg "\n", \
10158+ NIPQUAD(current->curr_ip) , ## args); \
10159+ else \
10160+ printk(KERN_INFO "grsec: " normal_msg "\n", ## args); \
10161+ spin_unlock(&grsec_audit_lock); \
10162+})
10163+
10164+#define security_learn(normal_msg,args...) \
10165+({ \
10166+ gr_add_learn_entry(normal_msg "\n", ## args); \
10167+})
10168+
10169+#endif
10170+
10171+#endif
10172diff -urN linux-2.4.21/include/linux/grmsg.h linux-2.4.21/include/linux/grmsg.h
10173--- linux-2.4.21/include/linux/grmsg.h 1969-12-31 19:00:00.000000000 -0500
10174+++ linux-2.4.21/include/linux/grmsg.h 2003-06-23 11:49:17.000000000 -0400
10175@@ -0,0 +1,101 @@
10176+#define DEFAULTSECMSG "(%.16s:%d) uid/euid:%d/%d gid/egid:%d/%d, parent (%.16s:%d) uid/euid:%d/%d gid/egid:%d/%d"
10177+#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"
10178+#define GR_PTRACE_ACL_MSG "denied ptrace of %.950s(%.16s:%d) by " DEFAULTSECMSG
10179+#define GR_IOPERM_MSG "denied use of ioperm() by " DEFAULTSECMSG
10180+#define GR_IOPL_MSG "denied use of iopl() by " DEFAULTSECMSG
10181+#define GR_SHMAT_ACL_MSG "denied attach of shared memory of UID %u, PID %d, ID %u by " DEFAULTSECMSG
10182+#define GR_UNIX_CHROOT_MSG "denied connect to abstract AF_UNIX socket outside of chroot by " DEFAULTSECMSG
10183+#define GR_SHMAT_CHROOT_MSG "denied attach of shared memory outside of chroot by " DEFAULTSECMSG
10184+#define GR_KMEM_MSG "attempted write to /dev/kmem by " DEFAULTSECMSG
10185+#define GR_PORT_OPEN_MSG "attempted open of /dev/port by " DEFAULTSECMSG
10186+#define GR_MEM_WRITE_MSG "attempted write of /dev/mem by " DEFAULTSECMSG
10187+#define GR_MEM_MMAP_MSG "attempted mmap write of /dev/[k]mem by " DEFAULTSECMSG
10188+#define GR_SYMLINK_MSG "not following symlink %.950s owned by %d.%d by " DEFAULTSECMSG
10189+#define GR_LEARN_AUDIT_MSG "%s:%u:%u:%u:%.4095s:%.4095s:%lu:%lu:%.4095s:%lu:%u.%u.%u.%u"
10190+#define GR_HIDDEN_ACL_MSG "%s access to hidden file %.950s by " DEFAULTSECMSG
10191+#define GR_OPEN_ACL_MSG "%s open of %.950s for%s%s by " DEFAULTSECMSG
10192+#define GR_CREATE_ACL_MSG "%s create of %.950s for%s%s by " DEFAULTSECMSG
10193+#define GR_FIFO_MSG "denied writing FIFO %.950s of %d.%d by " DEFAULTSECMSG
10194+#define GR_MKNOD_CHROOT_MSG "refused attempt to mknod %.950s from chroot by " DEFAULTSECMSG
10195+#define GR_MKNOD_ACL_MSG "%s mknod of %.950s by " DEFAULTSECMSG
10196+#define GR_UNIXCONNECT_ACL_MSG "%s connect to the unix domain socket %.950s by " DEFAULTSECMSG
10197+#define GR_MKDIR_ACL_MSG "%s mkdir of %.950s by " DEFAULTSECMSG
10198+#define GR_RMDIR_ACL_MSG "%s rmdir of %.950s by " DEFAULTSECMSG
10199+#define GR_UNLINK_ACL_MSG "%s unlink of %.950s by " DEFAULTSECMSG
10200+#define GR_SYMLINK_ACL_MSG "%s symlink from %.480s to %.480s by " DEFAULTSECMSG
10201+#define GR_HARDLINK_MSG "denied hardlink of %.930s (owned by %d.%d) to %.30s for " DEFAULTSECMSG
10202+#define GR_LINK_ACL_MSG "%s link of %.480s to %.480s by " DEFAULTSECMSG
10203+#define GR_INHERIT_ACL_MSG "successful inherit of %.480s's ACL for %.480s by " DEFAULTSECMSG
10204+#define GR_RENAME_ACL_MSG "%s rename of %.480s to %.480s by " DEFAULTSECMSG
10205+#define GR_PTRACE_EXEC_ACL_MSG "denied ptrace of %.950s by " DEFAULTSECMSG
10206+#define GR_NPROC_MSG "attempt to overstep process limit by " DEFAULTSECMSG
10207+#define GR_EXEC_ACL_MSG "%s execution of %.950s by " DEFAULTSECMSG
10208+#define GR_EXEC_TPE_MSG "denied untrusted exec of %.950s by " DEFAULTSECMSG
10209+#define GR_SEGVSTART_ACL_MSG "possible exploit bruteforcing on " DEFAULTSECMSG " Banning uid %u from login for %lu seconds"
10210+#define GR_SEGVNOSUID_ACL_MSG "possible exploit bruteforcing on " DEFAULTSECMSG " Banning execution of [%.16s:%lu] for %lu seconds"
10211+#define GR_MOUNT_CHROOT_MSG "denied attempt to mount %.30s as %.930s from chroot by " DEFAULTSECMSG
10212+#define GR_PIVOT_CHROOT_MSG "denied attempt to pivot_root from chroot by " DEFAULTSECMSG
10213+#define GR_TRUNCATE_ACL_MSG "%s truncate of %.950s by " DEFAULTSECMSG
10214+#define GR_ATIME_ACL_MSG "%s access time change of %.950s by " DEFAULTSECMSG
10215+#define GR_ACCESS_ACL_MSG "%s access of %.950s for%s%s%s by " DEFAULTSECMSG
10216+#define GR_CHROOT_CHROOT_MSG "denied attempt to double chroot to %.950s by " DEFAULTSECMSG
10217+#define GR_FCHMOD_ACL_MSG "%s fchmod of %.950s by " DEFAULTSECMSG
10218+#define GR_CHMOD_CHROOT_MSG "denied attempt to chmod +s %.950s by " DEFAULTSECMSG
10219+#define GR_CHMOD_ACL_MSG "%s chmod of %.950s by " DEFAULTSECMSG
10220+#define GR_CHROOT_FCHDIR_MSG "attempted fchdir outside of chroot to %.950s by " DEFAULTSECMSG
10221+#define GR_CHOWN_ACL_MSG "%s chown of %.950s by " DEFAULTSECMSG
10222+#define GR_WRITLIB_ACL_MSG "denied load of writable library %.950s by " DEFAULTSECMSG
10223+#define GR_INITF_ACL_MSG "init_variables() failed %s"
10224+#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"
10225+#define GR_DEV_ACL_MSG "/dev/grsec: being fed garbage %d bytes sent %d required"
10226+#define GR_SHUTS_ACL_MSG "shutdown auth success for " DEFAULTSECMSG
10227+#define GR_SHUTF_ACL_MSG "shutdown auth failure for " DEFAULTSECMSG
10228+#define GR_SHUTI_ACL_MSG "ignoring shutdown for disabled RBAC system for " DEFAULTSECMSG
10229+#define GR_SEGVMODS_ACL_MSG "segvmod auth success for " DEFAULTSECMSG
10230+#define GR_SEGVMODF_ACL_MSG "segvmod auth failure for " DEFAULTSECMSG
10231+#define GR_SEGVMODI_ACL_MSG "ignoring segvmod for disabled RBAC system for " DEFAULTSECMSG
10232+#define GR_ENABLE_ACL_MSG "Loaded %s"
10233+#define GR_ENABLEF_ACL_MSG "Unable to load %s for " DEFAULTSECMSG " RBAC system may already be enabled."
10234+#define GR_RELOADI_ACL_MSG "Ignoring reload request for disabled RBAC system"
10235+#define GR_RELOAD_ACL_MSG "Reloaded %s"
10236+#define GR_RELOADF_ACL_MSG "Failed reload of %s for " DEFAULTSECMSG
10237+#define GR_SPROLEI_ACL_MSG "Ignoring change to special role for disabled RBAC system for " DEFAULTSECMSG
10238+#define GR_SPROLES_ACL_MSG "successful change to special role %s (id %d) by " DEFAULTSECMSG
10239+#define GR_SPROLEL_ACL_MSG "special role %s (id %d) exited by " DEFAULTSECMSG
10240+#define GR_SPROLEF_ACL_MSG "special role %s failure for " DEFAULTSECMSG
10241+#define GR_INVMODE_ACL_MSG "Invalid mode %d by " DEFAULTSECMSG
10242+#define GR_MAXPW_ACL_MSG "Maximum pw attempts reached (%d), locking password authentication"
10243+#define GR_MAXROLEPW_ACL_MSG "Maximum pw attempts reached (%d) trying to auth to special role %s, locking auth for role of " DEFAULTSECMSG
10244+#define GR_PRIORITY_CHROOT_MSG "attempted priority change of process (%.16s:%d) by " DEFAULTSECMSG
10245+#define GR_CAPSET_CHROOT_MSG "denied capset of (%.16s:%d) within chroot by " DEFAULTSECMSG
10246+#define GR_FAILFORK_MSG "failed fork with errno %d by " DEFAULTSECMSG
10247+#define GR_NICE_CHROOT_MSG "attempted priority change by " DEFAULTSECMSG
10248+#define GR_UNISIGLOG_MSG "signal %d sent to " DEFAULTSECMSG
10249+#define GR_DUALSIGLOG_MSG "signal %d sent to " DEFAULTSECMSG " by " DEFAULTSECMSG
10250+#define GR_SIG_ACL_MSG "Attempted send of signal %d to protected task " DEFAULTSECMSG " by " DEFAULTSECMSG
10251+#define GR_SYSCTL_MSG "attempt to modify grsecurity sysctl value : %.32s by " DEFAULTSECMSG
10252+#define GR_SYSCTL_ACL_MSG "%s sysctl of %.950s for%s%s by " DEFAULTSECMSG
10253+#define GR_TIME_MSG "time set by " DEFAULTSECMSG
10254+#define GR_DEFACL_MSG "Fatal: Unable to find ACL for (%.16s:%d)"
10255+#define GR_MMAP_ACL_MSG "%s executable mmap of %.950s by " DEFAULTSECMSG
10256+#define GR_MPROTECT_ACL_MSG "%s executable mprotect of %.950s by " DEFAULTSECMSG
10257+#define GR_SOCK_MSG "attempted socket(%d,%d,%d) by " DEFAULTSECMSG
10258+#define GR_BIND_MSG "attempted bind() by " DEFAULTSECMSG
10259+#define GR_CONNECT_MSG "attempted connect by " DEFAULTSECMSG
10260+#define GR_BIND_ACL_MSG "attempted bind to %u.%u.%u.%u port %u sock type %u protocol %u by " DEFAULTSECMSG
10261+#define GR_CONNECT_ACL_MSG "attempted connect to %u.%u.%u.%u port %u sock type %u protocol %u by " DEFAULTSECMSG
10262+#define GR_IP_LEARN_MSG "%s:%u:%u:%u:%.4095s:%.4095s:%u.%u.%u.%u:%u:%u:%u:%u:%u.%u.%u.%u"
10263+#define GR_EXEC_CHROOT_MSG "exec of %.980s within chroot by process " DEFAULTSECMSG
10264+#define GR_CAP_ACL_MSG "use of %s denied for " DEFAULTSECMSG
10265+#define GR_REMOUNT_AUDIT_MSG "remount of %.30s by " DEFAULTSECMSG
10266+#define GR_UNMOUNT_AUDIT_MSG "unmount of %.30s by " DEFAULTSECMSG
10267+#define GR_MOUNT_AUDIT_MSG "mount %.30s to %.64s by " DEFAULTSECMSG
10268+#define GR_CHDIR_AUDIT_MSG "chdir to %.980s by " DEFAULTSECMSG
10269+#define GR_EXEC_AUDIT_MSG "exec of %.930s (%.63s) by " DEFAULTSECMSG
10270+#define GR_MSGQ_AUDIT_MSG "message queue created by " DEFAULTSECMSG
10271+#define GR_MSGQR_AUDIT_MSG "message queue of uid:%d euid:%d removed by " DEFAULTSECMSG
10272+#define GR_SEM_AUDIT_MSG "semaphore created by " DEFAULTSECMSG
10273+#define GR_SEMR_AUDIT_MSG "semaphore of uid:%d euid:%d removed by " DEFAULTSECMSG
10274+#define GR_SHM_AUDIT_MSG "shared memory of size %d created by " DEFAULTSECMSG
10275+#define GR_SHMR_AUDIT_MSG "shared memory of uid:%d euid:%d removed by " DEFAULTSECMSG
10276+#define GR_RESOURCE_MSG "attempted resource overstep by requesting %lu for %.16s against limit %lu by " DEFAULTSECMSG
10277diff -urN linux-2.4.21/include/linux/grsecurity.h linux-2.4.21/include/linux/grsecurity.h
10278--- linux-2.4.21/include/linux/grsecurity.h 1969-12-31 19:00:00.000000000 -0500
10279+++ linux-2.4.21/include/linux/grsecurity.h 2003-06-23 11:49:17.000000000 -0400
10280@@ -0,0 +1,173 @@
10281+#ifndef GR_SECURITY_H
10282+#define GR_SECURITY_H
10283+
10284+extern int gr_pid_is_chrooted(const struct task_struct *p);
10285+extern int gr_handle_chroot_nice(void);
10286+extern int gr_handle_chroot_sysctl(const int op);
10287+extern int gr_handle_chroot_capset(const struct task_struct *target);
10288+extern int gr_handle_chroot_setpriority(const struct task_struct *p,
10289+ const int niceval);
10290+extern int gr_chroot_fchdir(struct dentry *u_dentry, struct vfsmount *u_mnt);
10291+extern int gr_handle_chroot_chroot(const struct dentry *dentry,
10292+ const struct vfsmount *mnt);
10293+extern void gr_handle_chroot_caps(struct task_struct *task);
10294+extern void gr_handle_chroot_chdir(struct dentry *dentry, struct vfsmount *mnt);
10295+extern int gr_handle_chroot_chmod(const struct dentry *dentry,
10296+ const struct vfsmount *mnt, const int mode);
10297+extern int gr_handle_chroot_mknod(const struct dentry *dentry,
10298+ const struct vfsmount *mnt, const int mode);
10299+extern int gr_handle_chroot_mount(const struct dentry *dentry,
10300+ const struct vfsmount *mnt,
10301+ const char *dev_name);
10302+extern int gr_handle_chroot_pivot(void);
10303+extern int gr_handle_chroot_unix(const pid_t pid);
10304+
10305+extern int gr_handle_nproc(void);
10306+
10307+extern void gr_handle_ioperm(void);
10308+extern void gr_handle_iopl(void);
10309+
10310+extern int gr_tpe_allow(const struct file *file);
10311+
10312+extern int gr_random_pid(spinlock_t * pid_lock);
10313+
10314+extern void gr_log_forkfail(const int retval);
10315+extern void gr_log_timechange(void);
10316+extern void gr_log_signal(const int sig, const struct task_struct *t);
10317+extern void gr_log_chdir(const struct dentry *dentry,
10318+ const struct vfsmount *mnt);
10319+extern void gr_log_chroot_exec(const struct dentry *dentry,
10320+ const struct vfsmount *mnt);
10321+extern void gr_handle_exec_args(struct linux_binprm *bprm, char **argv);
10322+extern void gr_log_remount(const char *devname, const int retval);
10323+extern void gr_log_unmount(const char *devname, const int retval);
10324+extern void gr_log_mount(const char *from, const char *to, const int retval);
10325+extern void gr_log_msgget(const int ret, const int msgflg);
10326+extern void gr_log_msgrm(const uid_t uid, const uid_t cuid);
10327+extern void gr_log_semget(const int err, const int semflg);
10328+extern void gr_log_semrm(const uid_t uid, const uid_t cuid);
10329+extern void gr_log_shmget(const int err, const int shmflg, const size_t size);
10330+extern void gr_log_shmrm(const uid_t uid, const uid_t cuid);
10331+
10332+extern int gr_handle_follow_link(const struct inode *parent,
10333+ const struct inode *inode,
10334+ const struct dentry *dentry,
10335+ const struct vfsmount *mnt);
10336+extern int gr_handle_fifo(const struct dentry *dentry,
10337+ const struct vfsmount *mnt,
10338+ const struct dentry *dir, const int flag,
10339+ const int acc_mode);
10340+extern int gr_handle_hardlink(const struct dentry *dentry,
10341+ const struct vfsmount *mnt,
10342+ struct inode *inode,
10343+ const int mode, const char *to);
10344+
10345+extern int gr_is_capable(const int cap);
10346+extern void gr_learn_resource(const struct task_struct *task, const int limit,
10347+ const unsigned long wanted);
10348+extern void gr_copy_label(struct task_struct *tsk);
10349+extern void gr_handle_crash(struct task_struct *task, const int sig);
10350+extern int gr_handle_signal(const struct task_struct *p, const int sig);
10351+extern int gr_check_crash_uid(const uid_t uid);
10352+extern int gr_check_protected_task(const struct task_struct *task);
10353+extern int gr_acl_handle_mmap(const struct file *file,
10354+ const unsigned long prot);
10355+extern int gr_acl_handle_mprotect(const struct file *file,
10356+ const unsigned long prot);
10357+extern int gr_check_hidden_task(const struct task_struct *tsk);
10358+extern __u32 gr_acl_handle_truncate(const struct dentry *dentry,
10359+ const struct vfsmount *mnt);
10360+extern __u32 gr_acl_handle_utime(const struct dentry *dentry,
10361+ const struct vfsmount *mnt);
10362+extern __u32 gr_acl_handle_access(const struct dentry *dentry,
10363+ const struct vfsmount *mnt, const int fmode);
10364+extern __u32 gr_acl_handle_fchmod(const struct dentry *dentry,
10365+ const struct vfsmount *mnt, mode_t mode);
10366+extern __u32 gr_acl_handle_chmod(const struct dentry *dentry,
10367+ const struct vfsmount *mnt, mode_t mode);
10368+extern __u32 gr_acl_handle_chown(const struct dentry *dentry,
10369+ const struct vfsmount *mnt);
10370+extern int gr_handle_ptrace_exec(const struct dentry *dentry,
10371+ const struct vfsmount *mnt);
10372+extern int gr_handle_ptrace(struct task_struct *task, const long request);
10373+extern int gr_handle_mmap(const struct file *filp, const unsigned long prot);
10374+extern __u32 gr_acl_handle_execve(const struct dentry *dentry,
10375+ const struct vfsmount *mnt);
10376+extern int gr_check_crash_exec(const struct file *filp);
10377+extern int gr_acl_is_enabled(void);
10378+extern void gr_set_kernel_label(struct task_struct *task);
10379+extern void gr_set_role_label(struct task_struct *task, const uid_t uid,
10380+ const gid_t gid);
10381+extern void gr_set_proc_label(const struct dentry *dentry,
10382+ const struct vfsmount *mnt);
10383+extern __u32 gr_acl_handle_hidden_file(const struct dentry *dentry,
10384+ const struct vfsmount *mnt);
10385+extern __u32 gr_acl_handle_open(const struct dentry *dentry,
10386+ const struct vfsmount *mnt, const int fmode);
10387+extern __u32 gr_acl_handle_creat(const struct dentry *dentry,
10388+ const struct dentry *p_dentry,
10389+ const struct vfsmount *p_mnt, const int fmode,
10390+ const int imode);
10391+extern void gr_handle_create(const struct dentry *dentry,
10392+ const struct vfsmount *mnt);
10393+extern __u32 gr_acl_handle_mknod(const struct dentry *new_dentry,
10394+ const struct dentry *parent_dentry,
10395+ const struct vfsmount *parent_mnt,
10396+ const int mode);
10397+extern __u32 gr_acl_handle_mkdir(const struct dentry *new_dentry,
10398+ const struct dentry *parent_dentry,
10399+ const struct vfsmount *parent_mnt);
10400+extern __u32 gr_acl_handle_rmdir(const struct dentry *dentry,
10401+ const struct vfsmount *mnt);
10402+extern void gr_handle_delete(const ino_t ino, const kdev_t dev);
10403+extern __u32 gr_acl_handle_unlink(const struct dentry *dentry,
10404+ const struct vfsmount *mnt);
10405+extern __u32 gr_acl_handle_symlink(const struct dentry *new_dentry,
10406+ const struct dentry *parent_dentry,
10407+ const struct vfsmount *parent_mnt,
10408+ const char *from);
10409+extern __u32 gr_acl_handle_link(const struct dentry *new_dentry,
10410+ const struct dentry *parent_dentry,
10411+ const struct vfsmount *parent_mnt,
10412+ const struct dentry *old_dentry,
10413+ const struct vfsmount *old_mnt, const char *to);
10414+extern int gr_acl_handle_rename(struct dentry *new_dentry,
10415+ struct dentry *parent_dentry,
10416+ const struct vfsmount *parent_mnt,
10417+ struct dentry *old_dentry,
10418+ struct inode *old_parent_inode,
10419+ struct vfsmount *old_mnt, const char *newname);
10420+extern __u32 gr_check_link(const struct dentry *new_dentry,
10421+ const struct dentry *parent_dentry,
10422+ const struct vfsmount *parent_mnt,
10423+ const struct dentry *old_dentry,
10424+ const struct vfsmount *old_mnt);
10425+extern __u32 gr_acl_handle_filldir(const struct dentry *dentry,
10426+ const struct vfsmount *mnt, const ino_t ino);
10427+extern __u32 gr_acl_handle_unix(const struct dentry *dentry,
10428+ const struct vfsmount *mnt);
10429+extern void gr_acl_handle_exit(void);
10430+extern void gr_acl_handle_psacct(struct task_struct *task, const long code);
10431+extern int gr_acl_handle_procpidmem(const struct task_struct *task);
10432+extern __u32 gr_cap_rtnetlink(void);
10433+
10434+#ifdef CONFIG_GRKERNSEC
10435+extern void gr_handle_mem_write(void);
10436+extern void gr_handle_kmem_write(void);
10437+extern void gr_handle_open_port(void);
10438+extern int gr_handle_mem_mmap(const unsigned long offset,
10439+ struct vm_area_struct *vma);
10440+
10441+extern __u16 ip_randomid(void);
10442+extern __u32 ip_randomisn(void);
10443+extern __u32 arc4random(void);
10444+
10445+extern int grsec_enable_dmesg;
10446+extern int grsec_enable_randid;
10447+extern int grsec_enable_randisn;
10448+extern int grsec_enable_randsrc;
10449+extern int grsec_enable_randping;
10450+extern int grsec_enable_randrpc;
10451+#endif
10452+
10453+#endif
10454diff -urN linux-2.4.21/include/linux/mm.h linux-2.4.21/include/linux/mm.h
10455--- linux-2.4.21/include/linux/mm.h 2003-06-23 11:41:32.000000000 -0400
10456+++ linux-2.4.21/include/linux/mm.h 2003-06-23 11:49:17.000000000 -0400
10457@@ -22,6 +22,9 @@
10458 extern struct list_head active_list;
10459 extern struct list_head inactive_list;
10460
10461+extern void gr_learn_resource(const struct task_struct * task, const int limit,
10462+ const unsigned long wanted);
10463+
10464 #include <asm/page.h>
10465 #include <asm/pgtable.h>
10466 #include <asm/atomic.h>
10467@@ -639,6 +704,8 @@
10468 address &= PAGE_MASK;
10469 spin_lock(&vma->vm_mm->page_table_lock);
10470 grow = (vma->vm_start - address) >> PAGE_SHIFT;
10471+ gr_learn_resource(current, RLIMIT_STACK, vma->vm_end - address);
10472+ gr_learn_resource(current, RLIMIT_AS, (vma->vm_mm->total_vm + grow) << PAGE_SHIFT);
10473 if (vma->vm_end - address > current->rlim[RLIMIT_STACK].rlim_cur ||
10474 ((vma->vm_mm->total_vm + grow) << PAGE_SHIFT) > current->rlim[RLIMIT_AS].rlim_cur) {
10475 spin_unlock(&vma->vm_mm->page_table_lock);
10476diff -urN linux-2.4.21/include/linux/proc_fs.h linux-2.4.21/include/linux/proc_fs.h
10477--- linux-2.4.21/include/linux/proc_fs.h 2003-06-23 11:41:32.000000000 -0400
10478+++ linux-2.4.21/include/linux/proc_fs.h 2003-06-23 11:49:17.000000000 -0400
10479@@ -143,6 +143,9 @@
10480 extern struct proc_dir_entry *proc_mknod(const char *,mode_t,
10481 struct proc_dir_entry *,kdev_t);
10482 extern struct proc_dir_entry *proc_mkdir(const char *,struct proc_dir_entry *);
10483+#ifdef CONFIG_GRKERNSEC_PROC
10484+extern struct proc_dir_entry *proc_priv_mkdir(const char *, struct proc_dir_entry *);
10485+#endif
10486
10487 static inline struct proc_dir_entry *create_proc_read_entry(const char *name,
10488 mode_t mode, struct proc_dir_entry *base,
10489diff -urN linux-2.4.21/include/linux/sched.h linux-2.4.21/include/linux/sched.h
10490--- linux-2.4.21/include/linux/sched.h 2003-06-23 11:41:32.000000000 -0400
10491+++ linux-2.4.21/include/linux/sched.h 2003-06-23 11:49:17.000000000 -0400
10492@@ -27,6 +27,9 @@
10493 #include <linux/securebits.h>
10494 #include <linux/fs_struct.h>
10495
10496+extern int gr_is_capable(const int cap);
10497+extern int gr_pid_is_chrooted(const struct task_struct *p);
10498+
10499 struct exec_domain;
10500
10501 /*
10502@@ -415,6 +432,19 @@
10503
10504 /* journalling filesystem info */
10505 void *journal_info;
10506+
10507+#ifdef CONFIG_GRKERNSEC
10508+/* added by grsecurity's ACL system */
10509+ struct acl_subject_label *acl;
10510+ struct acl_role_label *role;
10511+ struct file *exec_file;
10512+ u32 curr_ip;
10513+ u16 acl_role_id;
10514+ u8 acl_sp_role:1;
10515+ u8 used_accept:1;
10516+ u8 used_connect:1;
10517+ u8 is_writable:1;
10518+#endif
10519 };
10520
10521 /*
10522@@ -549,6 +586,8 @@
10523 *p->pidhash_pprev = p->pidhash_next;
10524 }
10525
10526+#include <asm/current.h>
10527+
10528 static inline task_t *find_task_by_pid(int pid)
10529 {
10530 task_t *p, **htable = &pidhash[pid_hashfn(pid)];
10531@@ -556,6 +595,8 @@
10532 for(p = *htable; p && p->pid != pid; p = p->pidhash_next)
10533 ;
10534
10535+ if(gr_pid_is_chrooted(p)) p = NULL;
10536+
10537 return p;
10538 }
10539
10540@@ -576,8 +617,6 @@
10541 extern struct user_struct * alloc_uid(uid_t);
10542 extern void free_uid(struct user_struct *);
10543
10544-#include <asm/current.h>
10545-
10546 extern unsigned long volatile jiffies;
10547 extern unsigned long itimer_ticks;
10548 extern unsigned long itimer_next;
10549@@ -741,7 +780,7 @@
10550 static inline int capable(int cap)
10551 {
10552 #if 1 /* ok now */
10553- if (cap_raised(current->cap_effective, cap))
10554+ if (cap_raised(current->cap_effective, cap) && gr_is_capable(cap))
10555 #else
10556 if (cap_is_fs_cap(cap) ? current->fsuid == 0 : current->euid == 0)
10557 #endif
10558diff -urN linux-2.4.21/include/linux/sysctl.h linux-2.4.21/include/linux/sysctl.h
10559--- linux-2.4.21/include/linux/sysctl.h 2003-06-23 11:41:32.000000000 -0400
10560+++ linux-2.4.21/include/linux/sysctl.h 2003-06-23 11:49:17.000000000 -0400
10561@@ -125,6 +125,7 @@
10562 KERN_CADPID=54, /* int: PID of the process to notify on CAD */
10563 KERN_KDB=55, /* int: kdb on/off */
10564 KERN_CORE_PATTERN=56, /* string: pattern for core-files */
10565+ KERN_GRSECURITY=68, /* grsecurity */
10566 };
10567
10568
10569diff -urN linux-2.4.21/include/net/inetpeer.h linux-2.4.21/include/net/inetpeer.h
10570--- linux-2.4.21/include/net/inetpeer.h 2003-06-23 11:41:37.000000000 -0400
10571+++ linux-2.4.21/include/net/inetpeer.h 2003-06-23 11:49:17.000000000 -0400
10572@@ -13,6 +13,7 @@
10573 #include <linux/init.h>
10574 #include <linux/sched.h>
10575 #include <linux/spinlock.h>
10576+
10577 #include <asm/atomic.h>
10578
10579 struct inet_peer
10580@@ -34,6 +35,11 @@
10581 /* can be called with or without local BH being disabled */
10582 struct inet_peer *inet_getpeer(__u32 daddr, int create);
10583
10584+#ifdef CONFIG_GRKERNSEC_RANDID
10585+extern int grsec_enable_randid;
10586+extern __u16 ip_randomid(void);
10587+#endif
10588+
10589 extern spinlock_t inet_peer_unused_lock;
10590 extern struct inet_peer *inet_peer_unused_head;
10591 extern struct inet_peer **inet_peer_unused_tailp;
10592@@ -58,7 +64,14 @@
10593 __u16 id;
10594
10595 spin_lock_bh(&inet_peer_idlock);
10596- id = p->ip_id_count++;
10597+
10598+#ifdef CONFIG_GRKERNSEC_RANDID
10599+ if(grsec_enable_randid)
10600+ id = htons(ip_randomid());
10601+ else
10602+#endif
10603+ id = p->ip_id_count++;
10604+
10605 spin_unlock_bh(&inet_peer_idlock);
10606 return id;
10607 }
10608diff -urN linux-2.4.21/include/net/ip.h linux-2.4.21/include/net/ip.h
10609--- linux-2.4.21/include/net/ip.h 2003-06-23 11:41:37.000000000 -0400
10610+++ linux-2.4.21/include/net/ip.h 2003-06-23 11:49:17.000000000 -0400
10611@@ -64,6 +64,11 @@
10612 void (*destructor)(struct sock *);
10613 };
10614
10615+#ifdef CONFIG_GRKERNSEC_RANDID
10616+extern int grsec_enable_randid;
10617+extern __u16 ip_randomid(void);
10618+#endif
10619+
10620 extern struct ip_ra_chain *ip_ra_chain;
10621 extern rwlock_t ip_ra_lock;
10622
10623@@ -196,7 +201,13 @@
10624 * does not change, they drop every other packet in
10625 * a TCP stream using header compression.
10626 */
10627- iph->id = ((sk && sk->daddr) ? htons(sk->protinfo.af_inet.id++) : 0);
10628+
10629+#ifdef CONFIG_GRKERNSEC_RANDID
10630+ if(grsec_enable_randid)
10631+ iph->id = htons(ip_randomid());
10632+ else
10633+#endif
10634+ iph->id = ((sk && sk->daddr) ? htons(sk->protinfo.af_inet.id++) : 0);
10635 } else
10636 __ip_select_ident(iph, dst);
10637 }
10638diff -urN linux-2.4.21/init/main.c linux-2.4.21/init/main.c
10639--- linux-2.4.21/init/main.c 2003-06-23 11:41:31.000000000 -0400
10640+++ linux-2.4.21/init/main.c 2003-06-23 11:49:17.000000000 -0400
10641@@ -27,6 +27,7 @@
10642 #include <linux/iobuf.h>
10643 #include <linux/bootmem.h>
10644 #include <linux/tty.h>
10645+#include <linux/grsecurity.h>
10646
10647 #include <asm/io.h>
10648 #include <asm/bugs.h>
10649@@ -107,6 +108,8 @@
10650 extern void ipc_init(void);
10651 #endif
10652
10653+extern void grsecurity_init(void);
10654+
10655 /*
10656 * Boot command-line arguments
10657 */
10658@@ -546,6 +549,7 @@
10659 do_basic_setup();
10660
10661 prepare_namespace();
10662+ grsecurity_init();
10663
10664 /*
10665 * Ok, we have completed the initial bootup, and
10666diff -urN linux-2.4.21/ipc/msg.c linux-2.4.21/ipc/msg.c
10667--- linux-2.4.21/ipc/msg.c 2003-06-23 11:41:59.000000000 -0400
10668+++ linux-2.4.21/ipc/msg.c 2003-06-23 11:49:17.000000000 -0400
10669@@ -22,6 +22,7 @@
10670 #include <linux/init.h>
10671 #include <linux/proc_fs.h>
10672 #include <linux/list.h>
10673+#include <linux/grsecurity.h>
10674 #include <asm/uaccess.h>
10675 #include "util.h"
10676
10677@@ -326,6 +327,9 @@
10678 msg_unlock(id);
10679 }
10680 up(&msg_ids.sem);
10681+
10682+ gr_log_msgget(ret, msgflg);
10683+
10684 return ret;
10685 }
10686
10687@@ -560,6 +564,8 @@
10688 break;
10689 }
10690 case IPC_RMID:
10691+ gr_log_msgrm(ipcp->uid, ipcp->cuid);
10692+
10693 freeque (msqid);
10694 break;
10695 }
10696diff -urN linux-2.4.21/ipc/sem.c linux-2.4.21/ipc/sem.c
10697--- linux-2.4.21/ipc/sem.c 2003-06-23 11:41:59.000000000 -0400
10698+++ linux-2.4.21/ipc/sem.c 2003-06-23 11:49:17.000000000 -0400
10699@@ -62,6 +62,7 @@
10700 #include <linux/spinlock.h>
10701 #include <linux/init.h>
10702 #include <linux/proc_fs.h>
10703+#include <linux/grsecurity.h>
10704 #include <asm/uaccess.h>
10705 #include "util.h"
10706
10707@@ -181,6 +182,9 @@
10708 }
10709
10710 up(&sem_ids.sem);
10711+
10712+ gr_log_semget(err, semflg);
10713+
10714 return err;
10715 }
10716
10717@@ -728,6 +732,8 @@
10718
10719 switch(cmd){
10720 case IPC_RMID:
10721+ gr_log_semrm(ipcp->uid, ipcp->cuid);
10722+
10723 freeary(semid);
10724 err = 0;
10725 break;
10726diff -urN linux-2.4.21/ipc/shm.c linux-2.4.21/ipc/shm.c
10727--- linux-2.4.21/ipc/shm.c 2003-06-23 11:41:59.000000000 -0400
10728+++ linux-2.4.21/ipc/shm.c 2003-06-23 11:49:17.000000000 -0400
10729@@ -23,6 +23,7 @@
10730 #include <linux/mman.h>
10731 #include <linux/proc_fs.h>
10732 #include <asm/uaccess.h>
10733+#include <linux/grsecurity.h>
10734
10735 #include "util.h"
10736
10737@@ -38,8 +39,21 @@
10738 time_t shm_ctim;
10739 pid_t shm_cprid;
10740 pid_t shm_lprid;
10741+
10742+#ifdef CONFIG_GRKERNSEC
10743+ time_t shm_createtime;
10744+ pid_t shm_lapid;
10745+#endif
10746 };
10747
10748+#ifdef CONFIG_GRKERNSEC
10749+extern int gr_handle_shmat(const pid_t shm_cprid, const pid_t shm_lapid,
10750+ const time_t shm_createtime, const uid_t cuid,
10751+ const int shmid);
10752+extern int gr_chroot_shmat(const pid_t shm_cprid, const pid_t shm_lapid,
10753+ const time_t shm_createtime);
10754+#endif
10755+
10756 #define shm_flags shm_perm.mode
10757
10758 static struct file_operations shm_file_operations;
10759@@ -209,6 +223,9 @@
10760 shp->shm_lprid = 0;
10761 shp->shm_atim = shp->shm_dtim = 0;
10762 shp->shm_ctim = CURRENT_TIME;
10763+#ifdef CONFIG_GRKERNSEC
10764+ shp->shm_createtime = CURRENT_TIME;
10765+#endif
10766 shp->shm_segsz = size;
10767 shp->shm_nattch = 0;
10768 shp->id = shm_buildid(id,shp->shm_perm.seq);
10769@@ -254,6 +271,9 @@
10770 shm_unlock(id);
10771 }
10772 up(&shm_ids.sem);
10773+
10774+ gr_log_shmget(err, shmflg, size);
10775+
10776 return err;
10777 }
10778
10779@@ -509,6 +529,9 @@
10780 err=-EPERM;
10781 goto out_unlock_up;
10782 }
10783+
10784+ gr_log_shmrm(shp->shm_perm.uid, shp->shm_perm.cuid);
10785+
10786 if (shp->shm_nattch){
10787 shp->shm_flags |= SHM_DEST;
10788 /* Do not find it any more */
10789@@ -622,9 +645,28 @@
10790 shm_unlock(shmid);
10791 return -EACCES;
10792 }
10793+
10794+#ifdef CONFIG_GRKERNSEC
10795+ if (!gr_handle_shmat(shp->shm_cprid, shp->shm_lapid, shp->shm_createtime,
10796+ shp->shm_perm.cuid, shmid)) {
10797+ shm_unlock(shmid);
10798+ return -EACCES;
10799+ }
10800+
10801+ if (!gr_chroot_shmat(shp->shm_cprid, shp->shm_lapid, shp->shm_createtime)) {
10802+ shm_unlock(shmid);
10803+ return -EACCES;
10804+ }
10805+#endif
10806+
10807 file = shp->shm_file;
10808 size = file->f_dentry->d_inode->i_size;
10809 shp->shm_nattch++;
10810+
10811+#ifdef CONFIG_GRKERNSEC
10812+ shp->shm_lapid = current->pid;
10813+#endif
10814+
10815 shm_unlock(shmid);
10816
10817 down_write(&current->mm->mmap_sem);
10818diff -urN linux-2.4.21/kernel/capability.c linux-2.4.21/kernel/capability.c
10819--- linux-2.4.21/kernel/capability.c 2003-06-23 11:41:31.000000000 -0400
10820+++ linux-2.4.21/kernel/capability.c 2003-06-23 11:49:17.000000000 -0400
10821@@ -7,6 +7,7 @@
10822
10823 #include <linux/mm.h>
10824 #include <asm/uaccess.h>
10825+#include <linux/grsecurity.h>
10826
10827 kernel_cap_t cap_bset = CAP_INIT_EFF_SET;
10828
10829@@ -168,6 +169,10 @@
10830 target = current;
10831 }
10832
10833+ if (gr_handle_chroot_capset(target)) {
10834+ error = -ESRCH;
10835+ goto out;
10836+ }
10837
10838 /* verify restrictions on target's new Inheritable set */
10839 if (!cap_issubset(inheritable,
10840diff -urN linux-2.4.21/kernel/exit.c linux-2.4.21/kernel/exit.c
10841--- linux-2.4.21/kernel/exit.c 2003-06-23 11:41:31.000000000 -0400
10842+++ linux-2.4.21/kernel/exit.c 2003-06-23 11:49:17.000000000 -0400
10843@@ -16,6 +16,7 @@
10844 #ifdef CONFIG_BSD_PROCESS_ACCT
10845 #include <linux/acct.h>
10846 #endif
10847+#include <linux/grsecurity.h>
10848
10849 #include <asm/uaccess.h>
10850 #include <asm/pgtable.h>
10851@@ -165,12 +165,21 @@
10852 {
10853 write_lock_irq(&tasklist_lock);
10854
10855+#ifdef CONFIG_GRKERNSEC
10856+ if (current->exec_file) {
10857+ fput(current->exec_file);
10858+ current->exec_file = NULL;
10859+ }
10860+#endif
10861+
10862 /* Reparent to init */
10863 REMOVE_LINKS(current);
10864 current->p_pptr = child_reaper;
10865 current->p_opptr = child_reaper;
10866 SET_LINKS(current);
10867
10868+ gr_set_kernel_label(current);
10869+
10870 /* Set the exit signal to SIGCHLD so we signal init on exit */
10871 current->exit_signal = SIGCHLD;
10872
10873@@ -439,6 +440,10 @@
10874 #ifdef CONFIG_BSD_PROCESS_ACCT
10875 acct_process(code);
10876 #endif
10877+
10878+ gr_acl_handle_psacct(tsk, code);
10879+ gr_acl_handle_exit();
10880+
10881 __exit_mm(tsk);
10882
10883 lock_kernel();
10884diff -urN linux-2.4.21/kernel/fork.c linux-2.4.21/kernel/fork.c
10885--- linux-2.4.21/kernel/fork.c 2003-06-23 11:41:31.000000000 -0400
10886+++ linux-2.4.21/kernel/fork.c 2003-06-23 11:49:17.000000000 -0400
10887@@ -22,6 +22,7 @@
dd08e044
JR
10888 #include <linux/personality.h>
10889 #include <linux/compiler.h>
79ee5d42 10890 #include <linux/mman.h>
dd08e044
JR
10891+#include <linux/grsecurity.h>
10892
10893 #include <asm/pgtable.h>
10894 #include <asm/pgalloc.h>
10895@@ -93,6 +94,10 @@
10896 if (flags & CLONE_PID)
10897 return current->pid;
10898
10899+ pid = gr_random_pid(&lastpid_lock);
10900+ if (pid)
10901+ return pid;
10902+
10903 spin_lock(&lastpid_lock);
10904 beginpid = last_pid;
10905 if((++last_pid) & 0xffff8000) {
10906@@ -635,6 +640,9 @@
10907 * friends to set the per-user process limit to something lower
10908 * than the amount of processes root is running. -- Rik
10909 */
10910+
10911+ gr_learn_resource(p, RLIMIT_NPROC, atomic_read(&p->user->processes));
10912+
10913 if (atomic_read(&p->user->processes) >= p->rlim[RLIMIT_NPROC].rlim_cur
10914 && !capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RESOURCE))
10915 goto bad_fork_free;
10916@@ -719,6 +727,7 @@
10917 retval = copy_thread(0, clone_flags, stack_start, stack_size, p, regs);
10918 if (retval)
10919 goto bad_fork_cleanup_namespace;
10920+ gr_copy_label(p);
10921 p->semundo = NULL;
10922
10923 /* Our parent execution domain becomes current domain
10924@@ -804,6 +813,9 @@
10925 free_uid(p->user);
10926 bad_fork_free:
10927 free_task_struct(p);
10928+
10929+ gr_log_forkfail(retval);
10930+
10931 goto fork_out;
10932 }
10933
10934diff -urN linux-2.4.21/kernel/ksyms.c linux-2.4.21/kernel/ksyms.c
10935--- linux-2.4.21/kernel/ksyms.c 2003-06-23 11:41:31.000000000 -0400
10936+++ linux-2.4.21/kernel/ksyms.c 2003-06-23 11:49:17.000000000 -0400
10937@@ -48,6 +48,7 @@
10938 #include <linux/completion.h>
10939 #include <linux/seq_file.h>
10940 #include <linux/dnotify.h>
10941+#include <linux/grsecurity.h>
10942 #include <asm/checksum.h>
10943
10944 #if defined(CONFIG_PROC_FS)
10945@@ -585,3 +586,9 @@
10946
10947 /* debug */
10948 EXPORT_SYMBOL(dump_stack);
10949+
10950+/* grsecurity */
10951+EXPORT_SYMBOL(gr_is_capable);
10952+EXPORT_SYMBOL(gr_pid_is_chrooted);
10953+EXPORT_SYMBOL(gr_learn_resource);
10954+EXPORT_SYMBOL(gr_set_kernel_label);
10955diff -urN linux-2.4.21/kernel/module.c linux-2.4.21/kernel/module.c
10956--- linux-2.4.21/kernel/module.c 2003-06-23 11:41:31.000000000 -0400
10957+++ linux-2.4.21/kernel/module.c 2003-06-23 11:49:17.000000000 -0400
10958@@ -887,6 +887,11 @@
10959 struct module *mod;
10960 int err;
10961
10962+#ifdef CONFIG_GRKERNSEC_HIDESYM
10963+ if (!capable(CAP_SYS_MODULE))
10964+ return -EPERM;
10965+#endif
10966+
10967 lock_kernel();
10968 if (name_user == NULL)
10969 mod = &kernel_module;
10970@@ -956,6 +961,11 @@
10971 int i;
10972 struct kernel_sym ksym;
10973
10974+#ifdef CONFIG_GRKERNSEC_HIDESYM
10975+ if (!capable(CAP_SYS_MODULE))
10976+ return 0;
10977+#endif
10978+
10979 lock_kernel();
10980 for (mod = module_list, i = 0; mod; mod = mod->next) {
10981 /* include the count for the module name! */
10982diff -urN linux-2.4.21/kernel/printk.c linux-2.4.21/kernel/printk.c
10983--- linux-2.4.21/kernel/printk.c 2003-06-23 11:41:31.000000000 -0400
10984+++ linux-2.4.21/kernel/printk.c 2003-06-23 11:49:17.000000000 -0400
10985@@ -26,6 +26,7 @@
10986 #include <linux/interrupt.h> /* For in_interrupt() */
10987 #include <linux/config.h>
10988 #include <linux/delay.h>
10989+#include <linux/grsecurity.h>
10990
10991 #include <asm/uaccess.h>
10992
10993@@ -294,6 +295,11 @@
10994
10995 asmlinkage long sys_syslog(int type, char * buf, int len)
10996 {
10997+#ifdef CONFIG_GRKERNSEC_DMESG
10998+ if (!capable(CAP_SYS_ADMIN) && grsec_enable_dmesg)
10999+ return -EPERM;
11000+ else
11001+#endif
11002 if ((type != 3) && !capable(CAP_SYS_ADMIN))
11003 return -EPERM;
11004 return do_syslog(type, buf, len);
11005diff -urN linux-2.4.21/kernel/sched.c linux-2.4.21/kernel/sched.c
11006--- linux-2.4.21/kernel/sched.c 2003-06-23 11:41:31.000000000 -0400
11007+++ linux-2.4.21/kernel/sched.c 2003-06-23 11:49:17.000000000 -0400
11008@@ -29,11 +30,13 @@
11009 #include <linux/nmi.h>
11010 #include <linux/interrupt.h>
11011 #include <linux/init.h>
11012+#include <linux/file.h>
11013 #include <asm/uaccess.h>
11014 #include <linux/smp_lock.h>
11015 #include <asm/mmu_context.h>
11016 #include <linux/kernel_stat.h>
11017 #include <linux/completion.h>
11018+#include <linux/grsecurity.h>
11019
11020 /*
11021 * Convert user-nice values [ -20 ... 0 ... 19 ]
11022@@ -910,6 +912,9 @@
11023 return -EPERM;
11024 if (increment < -40)
11025 increment = -40;
11026+
11027+ if (gr_handle_chroot_nice())
11028+ return -EPERM;
11029 }
11030 if (increment > 40)
11031 increment = 40;
11032diff -urN linux-2.4.21/kernel/signal.c linux-2.4.21/kernel/signal.c
11033--- linux-2.4.21/kernel/signal.c 2003-06-23 11:41:31.000000000 -0400
11034+++ linux-2.4.21/kernel/signal.c 2003-06-23 11:49:17.000000000 -0400
11035@@ -13,6 +13,8 @@
11036 #include <linux/smp_lock.h>
11037 #include <linux/init.h>
11038 #include <linux/sched.h>
11039+#include <linux/fs.h>
11040+#include <linux/grsecurity.h>
11041
11042 #include <asm/uaccess.h>
11043
11044@@ -554,6 +556,8 @@
11045 if (!sig || !t->sig)
11046 goto out_nolock;
11047
11048+ gr_log_signal(sig, t);
11049+
11050 spin_lock_irqsave(&t->sigmask_lock, flags);
11051 handle_stop_signal(sig, t);
11052
11053@@ -603,6 +607,8 @@
11054 recalc_sigpending(t);
11055 spin_unlock_irqrestore(&t->sigmask_lock, flags);
11056
11057+ gr_handle_crash(t, sig);
11058+
11059 return send_sig_info(sig, info, t);
11060 }
11061
11062@@ -622,9 +628,13 @@
11063 read_lock(&tasklist_lock);
11064 for_each_task(p) {
11065 if (p->pgrp == pgrp && thread_group_leader(p)) {
11066- int err = send_sig_info(sig, info, p);
11067- if (retval)
11068- retval = err;
11069+ if (gr_handle_signal(p, sig))
11070+ retval = -EPERM;
11071+ else {
11072+ int err = send_sig_info(sig, info, p);
11073+ if (retval)
11074+ retval = err;
11075+ }
11076 }
11077 }
11078 read_unlock(&tasklist_lock);
11079@@ -675,7 +685,10 @@
11080 if (tg)
11081 p = tg;
11082 }
11083- error = send_sig_info(sig, info, p);
11084+ if (gr_handle_signal(p, sig))
11085+ error = -EPERM;
11086+ else
11087+ error = send_sig_info(sig, info, p);
11088 }
11089 read_unlock(&tasklist_lock);
11090 return error;
11091@@ -700,10 +713,14 @@
11092 read_lock(&tasklist_lock);
11093 for_each_task(p) {
11094 if (p->pid > 1 && p != current && thread_group_leader(p)) {
11095- int err = send_sig_info(sig, info, p);
11096- ++count;
11097- if (err != -EPERM)
11098- retval = err;
11099+ if (gr_handle_signal(p, sig))
11100+ retval = -EPERM;
11101+ else {
11102+ int err = send_sig_info(sig, info, p);
11103+ ++count;
11104+ if (err != -EPERM)
11105+ retval = err;
11106+ }
11107 }
11108 }
11109 read_unlock(&tasklist_lock);
11110diff -urN linux-2.4.21/kernel/sys.c linux-2.4.21/kernel/sys.c
11111--- linux-2.4.21/kernel/sys.c 2003-06-23 11:41:31.000000000 -0400
11112+++ linux-2.4.21/kernel/sys.c 2003-06-23 11:49:17.000000000 -0400
11113@@ -4,6 +4,7 @@
11114 * Copyright (C) 1991, 1992 Linus Torvalds
11115 */
11116
11117+#include <linux/config.h>
11118 #include <linux/module.h>
11119 #include <linux/mm.h>
11120 #include <linux/utsname.h>
11121@@ -14,6 +15,7 @@
11122 #include <linux/prctl.h>
11123 #include <linux/init.h>
11124 #include <linux/highuid.h>
11125+#include <linux/grsecurity.h>
11126
11127 #include <asm/uaccess.h>
11128 #include <asm/io.h>
11129@@ -239,6 +241,12 @@
11130 }
11131 if (error == -ESRCH)
11132 error = 0;
11133+
11134+ if (gr_handle_chroot_setpriority(p, niceval)) {
11135+ read_unlock(&tasklist_lock);
11136+ return -ESRCH;
11137+ }
11138+
11139 if (niceval < task_nice(p) && !capable(CAP_SYS_NICE))
11140 error = -EACCES;
11141 else
11142@@ -425,6 +433,9 @@
11143 if (rgid != (gid_t) -1 ||
11144 (egid != (gid_t) -1 && egid != old_rgid))
11145 current->sgid = new_egid;
11146+
11147+ gr_set_role_label(current, current->uid, new_rgid);
11148+
11149 current->fsgid = new_egid;
11150 current->egid = new_egid;
11151 current->gid = new_rgid;
11152@@ -447,6 +458,9 @@
11153 current->mm->dumpable=0;
11154 wmb();
11155 }
11156+
11157+ gr_set_role_label(current, current->uid, gid);
11158+
11159 current->gid = current->egid = current->sgid = current->fsgid = gid;
11160 }
11161 else if ((gid == current->gid) || (gid == current->sgid))
11162@@ -530,6 +544,9 @@
11163 current->mm->dumpable = 0;
11164 wmb();
11165 }
11166+
11167+ gr_set_role_label(current, new_ruid, current->gid);
11168+
11169 current->uid = new_ruid;
11170 current->user = new_user;
11171 free_uid(old_user);
11172@@ -626,6 +643,9 @@
11173 } else if ((uid != current->uid) && (uid != new_suid))
11174 return -EPERM;
11175
11176+ if (gr_check_crash_uid(uid))
11177+ return -EPERM;
11178+
11179 if (old_euid != uid)
11180 {
11181 current->mm->dumpable = 0;
11182@@ -722,8 +742,10 @@
11183 current->egid = egid;
11184 }
11185 current->fsgid = current->egid;
11186- if (rgid != (gid_t) -1)
11187+ if (rgid != (gid_t) -1) {
11188+ gr_set_role_label(current, current->uid, rgid);
11189 current->gid = rgid;
11190+ }
11191 if (sgid != (gid_t) -1)
11192 current->sgid = sgid;
11193 return 0;
11194@@ -1140,6 +1162,10 @@
11195 if(copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
11196 return -EFAULT;
11197 old_rlim = current->rlim + resource;
11198+
11199+ if (old_rlim->rlim_max < old_rlim->rlim_cur)
11200+ return -EINVAL;
11201+
11202 if (((new_rlim.rlim_cur > old_rlim->rlim_max) ||
11203 (new_rlim.rlim_max > old_rlim->rlim_max)) &&
11204 !capable(CAP_SYS_RESOURCE))
11205diff -urN linux-2.4.21/kernel/sysctl.c linux-2.4.21/kernel/sysctl.c
11206--- linux-2.4.21/kernel/sysctl.c 2003-06-23 11:41:31.000000000 -0400
11207+++ linux-2.4.21/kernel/sysctl.c 2003-06-23 11:49:17.000000000 -0400
11208@@ -38,6 +38,13 @@
11209 #endif
11210
11211 #if defined(CONFIG_SYSCTL)
11212+#include <linux/grsecurity.h>
11213+#include <linux/grinternal.h>
11214+
11215+extern __u32 gr_handle_sysctl(const ctl_table * table, const void *oldval,
11216+ const void *newval);
11217+extern int gr_handle_sysctl_mod(const char *dirname, const char *name, const int op);
11218+extern int gr_handle_chroot_sysctl(const int op);
11219
11220 /* External variables not in a header file. */
11221 extern int panic_timeout;
11222@@ -119,6 +126,8 @@
11223 static ctl_table dev_table[];
11224 extern ctl_table random_table[];
11225
11226+static ctl_table grsecurity_table[];
11227+
11228 /* /proc declarations: */
11229
11230 #ifdef CONFIG_PROC_FS
11231@@ -259,8 +268,195 @@
11232 {KERN_S390_USER_DEBUG_LOGGING,"userprocess_debug",
11233 &sysctl_userprocess_debug,sizeof(int),0644,NULL,&proc_dointvec},
11234 #endif
11235+#ifdef CONFIG_GRKERNSEC_SYSCTL
11236+ {KERN_GRSECURITY, "grsecurity", NULL, 0, 0555, grsecurity_table},
11237+#endif
11238+ {0}
11239+};
11240+
11241+#ifdef CONFIG_GRKERNSEC_SYSCTL
11242+enum {GS_LINK=1, GS_FIFO, GS_EXECVE, GS_EXECLOG, GS_SIGNAL,
11243+GS_FORKFAIL, GS_TIME, GS_CHROOT_SHMAT, GS_CHROOT_UNIX, GS_CHROOT_MNT,
11244+GS_CHROOT_FCHDIR, GS_CHROOT_DBL, GS_CHROOT_PVT, GS_CHROOT_CD, GS_CHROOT_CM,
11245+GS_CHROOT_MK, GS_CHROOT_NI, GS_CHROOT_EXECLOG, GS_CHROOT_CAPS,
11246+GS_CHROOT_SYSCTL, GS_TPE, GS_TPE_GID, GS_TPE_ALL, GS_SIDCAPS,
11247+GS_RANDPID, GS_RANDID, GS_RANDSRC, GS_RANDISN,
11248+GS_RANDPING, GS_SOCKET_ALL, GS_SOCKET_ALL_GID, GS_SOCKET_CLIENT,
11249+GS_SOCKET_CLIENT_GID, GS_SOCKET_SERVER, GS_SOCKET_SERVER_GID, GS_TTY, GS_TTYS,
11250+GS_PTY, GS_GROUP, GS_GID, GS_ACHDIR, GS_AMOUNT, GS_AIPC, GS_DMSG, GS_RANDRPC,
11251+GS_FINDTASK, GS_LOCK};
11252+
11253+static ctl_table grsecurity_table[] = {
11254+#ifdef CONFIG_GRKERNSEC_LINK
11255+ {GS_LINK, "linking_restrictions", &grsec_enable_link, sizeof (int),
11256+ 0600, NULL, &proc_dointvec},
11257+#endif
11258+#ifdef CONFIG_GRKERNSEC_FIFO
11259+ {GS_FIFO, "fifo_restrictions", &grsec_enable_fifo, sizeof (int),
11260+ 0600, NULL, &proc_dointvec},
11261+#endif
11262+#ifdef CONFIG_GRKERNSEC_EXECVE
11263+ {GS_EXECVE, "execve_limiting", &grsec_enable_execve, sizeof (int),
11264+ 0600, NULL, &proc_dointvec},
11265+#endif
11266+#ifdef CONFIG_GRKERNSEC_EXECLOG
11267+ {GS_EXECLOG, "exec_logging", &grsec_enable_execlog, sizeof (int),
11268+ 0600, NULL, &proc_dointvec},
11269+#endif
11270+#ifdef CONFIG_GRKERNSEC_SIGNAL
11271+ {GS_SIGNAL, "signal_logging", &grsec_enable_signal, sizeof (int),
11272+ 0600, NULL, &proc_dointvec},
11273+#endif
11274+#ifdef CONFIG_GRKERNSEC_FORKFAIL
11275+ {GS_FORKFAIL, "forkfail_logging", &grsec_enable_forkfail, sizeof (int),
11276+ 0600, NULL, &proc_dointvec},
11277+#endif
11278+#ifdef CONFIG_GRKERNSEC_TIME
11279+ {GS_TIME, "timechange_logging", &grsec_enable_time, sizeof (int),
11280+ 0600, NULL, &proc_dointvec},
11281+#endif
11282+#ifdef CONFIG_GRKERNSEC_CHROOT_SHMAT
11283+ {GS_CHROOT_SHMAT, "chroot_deny_shmat", &grsec_enable_chroot_shmat, sizeof (int),
11284+ 0600, NULL, &proc_dointvec},
11285+#endif
11286+#ifdef CONFIG_GRKERNSEC_CHROOT_UNIX
11287+ {GS_CHROOT_UNIX, "chroot_deny_unix", &grsec_enable_chroot_unix, sizeof(int),
11288+ 0600, NULL, &proc_dointvec},
11289+#endif
11290+#ifdef CONFIG_GRKERNSEC_CHROOT_MOUNT
11291+ {GS_CHROOT_MNT, "chroot_deny_mount", &grsec_enable_chroot_mount, sizeof (int),
11292+ 0600, NULL, &proc_dointvec},
11293+#endif
11294+#ifdef CONFIG_GRKERNSEC_CHROOT_FCHDIR
11295+ {GS_CHROOT_FCHDIR, "chroot_deny_fchdir", &grsec_enable_chroot_fchdir, sizeof (int),
11296+ 0600, NULL, &proc_dointvec},
11297+#endif
11298+#ifdef CONFIG_GRKERNSEC_CHROOT_DOUBLE
11299+ {GS_CHROOT_DBL, "chroot_deny_chroot", &grsec_enable_chroot_double, sizeof (int),
11300+ 0600, NULL, &proc_dointvec},
11301+#endif
11302+#ifdef CONFIG_GRKERNSEC_CHROOT_PIVOT
11303+ {GS_CHROOT_PVT, "chroot_deny_pivot", &grsec_enable_chroot_pivot, sizeof (int),
11304+ 0600, NULL, &proc_dointvec},
11305+#endif
11306+#ifdef CONFIG_GRKERNSEC_CHROOT_CHDIR
11307+ {GS_CHROOT_CD, "chroot_enforce_chdir", &grsec_enable_chroot_chdir, sizeof (int),
11308+ 0600, NULL, &proc_dointvec},
11309+#endif
11310+#ifdef CONFIG_GRKERNSEC_CHROOT_CHMOD
11311+ {GS_CHROOT_CM, "chroot_deny_chmod", &grsec_enable_chroot_chmod, sizeof (int),
11312+ 0600, NULL, &proc_dointvec},
11313+#endif
11314+#ifdef CONFIG_GRKERNSEC_CHROOT_MKNOD
11315+ {GS_CHROOT_MK, "chroot_deny_mknod", &grsec_enable_chroot_mknod, sizeof (int),
11316+ 0600, NULL, &proc_dointvec},
11317+#endif
11318+#ifdef CONFIG_GRKERNSEC_CHROOT_NICE
11319+ {GS_CHROOT_NI, "chroot_restrict_nice", &grsec_enable_chroot_nice, sizeof (int),
11320+ 0600, NULL, &proc_dointvec},
11321+#endif
11322+#ifdef CONFIG_GRKERNSEC_CHROOT_EXECLOG
11323+ {GS_CHROOT_EXECLOG, "chroot_execlog",
11324+ &grsec_enable_chroot_execlog, sizeof (int),
11325+ 0600, NULL, &proc_dointvec},
11326+#endif
11327+#ifdef CONFIG_GRKERNSEC_CHROOT_CAPS
11328+ {GS_CHROOT_CAPS, "chroot_caps", &grsec_enable_chroot_caps, sizeof (int),
11329+ 0600, NULL, &proc_dointvec},
11330+#endif
11331+#ifdef CONFIG_GRKERNSEC_CHROOT_SYSCTL
11332+ {GS_CHROOT_SYSCTL, "chroot_deny_sysctl", &grsec_enable_chroot_sysctl, sizeof (int),
11333+ 0600, NULL, &proc_dointvec},
11334+#endif
11335+#ifdef CONFIG_GRKERNSEC_TPE
11336+ {GS_TPE, "tpe", &grsec_enable_tpe, sizeof (int),
11337+ 0600, NULL, &proc_dointvec},
11338+ {GS_TPE_GID, "tpe_gid", &grsec_tpe_gid, sizeof (int),
11339+ 0600, NULL, &proc_dointvec},
11340+#endif
11341+#ifdef CONFIG_GRKERNSEC_TPE_ALL
11342+ {GS_TPE_ALL, "tpe_restrict_all", &grsec_enable_tpe_all, sizeof (int),
11343+ 0600, NULL, &proc_dointvec},
11344+#endif
11345+#ifdef CONFIG_GRKERNSEC_RANDPID
11346+ {GS_RANDPID, "rand_pids", &grsec_enable_randpid, sizeof (int),
11347+ 0600, NULL, &proc_dointvec},
11348+#endif
11349+#ifdef CONFIG_GRKERNSEC_RANDID
11350+ {GS_RANDID, "rand_ip_ids", &grsec_enable_randid, sizeof (int),
11351+ 0600, NULL, &proc_dointvec},
11352+#endif
11353+#ifdef CONFIG_GRKERNSEC_RANDSRC
11354+ {GS_RANDSRC, "rand_tcp_src_ports", &grsec_enable_randsrc, sizeof (int),
11355+ 0600, NULL, &proc_dointvec},
11356+#endif
11357+#ifdef CONFIG_GRKERNSEC_RANDISN
11358+ {GS_RANDISN, "rand_isns", &grsec_enable_randisn, sizeof (int),
11359+ 0600, NULL, &proc_dointvec},
11360+#endif
11361+#ifdef CONFIG_GRKERNSEC_RANDPING
11362+ {GS_RANDPING, "altered_pings", &grsec_enable_randping, sizeof (int),
11363+ 0600, NULL, &proc_dointvec},
11364+#endif
11365+#ifdef CONFIG_GRKERNSEC_SOCKET_ALL
11366+ {GS_SOCKET_ALL, "socket_all", &grsec_enable_socket_all, sizeof (int),
11367+ 0600, NULL, &proc_dointvec},
11368+ {GS_SOCKET_ALL_GID, "socket_all_gid",
11369+ &grsec_socket_all_gid, sizeof (int),
11370+ 0600, NULL, &proc_dointvec},
11371+#endif
11372+#ifdef CONFIG_GRKERNSEC_SOCKET_CLIENT
11373+ {GS_SOCKET_CLIENT, "socket_client",
11374+ &grsec_enable_socket_client, sizeof (int),
11375+ 0600, NULL, &proc_dointvec},
11376+ {GS_SOCKET_CLIENT_GID, "socket_client_gid",
11377+ &grsec_socket_client_gid, sizeof (int),
11378+ 0600, NULL, &proc_dointvec},
11379+#endif
11380+#ifdef CONFIG_GRKERNSEC_SOCKET_SERVER
11381+ {GS_SOCKET_SERVER, "socket_server",
11382+ &grsec_enable_socket_server, sizeof (int),
11383+ 0600, NULL, &proc_dointvec},
11384+ {GS_SOCKET_SERVER_GID, "socket_server_gid",
11385+ &grsec_socket_server_gid, sizeof (int),
11386+ 0600, NULL, &proc_dointvec},
11387+#endif
11388+#ifdef CONFIG_GRKERNSEC_AUDIT_GROUP
11389+ {GS_GROUP, "audit_group", &grsec_enable_group, sizeof (int),
11390+ 0600, NULL, &proc_dointvec},
11391+ {GS_GID, "audit_gid",
11392+ &grsec_audit_gid, sizeof (int),
11393+ 0600, NULL, &proc_dointvec},
11394+#endif
11395+#ifdef CONFIG_GRKERNSEC_AUDIT_CHDIR
11396+ {GS_ACHDIR, "audit_chdir", &grsec_enable_chdir, sizeof (int),
11397+ 0600, NULL, &proc_dointvec},
11398+#endif
11399+#ifdef CONFIG_GRKERNSEC_AUDIT_MOUNT
11400+ {GS_AMOUNT, "audit_mount", &grsec_enable_mount, sizeof (int),
11401+ 0600, NULL, &proc_dointvec},
11402+#endif
11403+#ifdef CONFIG_GRKERNSEC_AUDIT_IPC
11404+ {GS_AIPC, "audit_ipc", &grsec_enable_audit_ipc, sizeof (int),
11405+ 0600, NULL, &proc_dointvec},
11406+#endif
11407+#ifdef CONFIG_GRKERNSEC_DMESG
11408+ {GS_AIPC, "dmesg", &grsec_enable_dmesg, sizeof (int),
11409+ 0600, NULL, &proc_dointvec},
11410+#endif
11411+#ifdef CONFIG_GRKERNSEC_RANDRPC
11412+ {GS_RANDRPC, "rand_rpc", &grsec_enable_randrpc, sizeof (int),
11413+ 0600, NULL, &proc_dointvec},
11414+#endif
11415+#ifdef CONFIG_GRKERNSEC_CHROOT_FINDTASK
11416+ {GS_FINDTASK, "chroot_findtask", &grsec_enable_chroot_findtask,
11417+ sizeof (int), 0600, NULL, &proc_dointvec},
11418+#endif
11419+ {GS_LOCK, "grsec_lock", &grsec_lock, sizeof (int), 0600, NULL,
11420+ &proc_dointvec},
11421 {0}
11422 };
11423+#endif
11424
11425 static ctl_table vm_table[] = {
11426 {VM_BDFLUSH, "bdflush", &bdf_prm, 9*sizeof(int), 0644, NULL,
11427@@ -395,6 +591,11 @@
11428
11429 static inline int ctl_perm(ctl_table *table, int op)
11430 {
11431+ if (gr_handle_sysctl_mod(table->de->parent->name, table->de->name, op))
11432+ return -EACCES;
11433+ if (gr_handle_chroot_sysctl(op))
11434+ return -EACCES;
11435+
11436 return test_perm(table->mode, op);
11437 }
11438
11439@@ -428,6 +629,10 @@
11440 table = table->child;
11441 goto repeat;
11442 }
11443+
11444+ if (!gr_handle_sysctl(table, oldval, newval))
11445+ return -EACCES;
11446+
11447 error = do_sysctl_strategy(table, name, nlen,
11448 oldval, oldlenp,
11449 newval, newlen, context);
11450diff -urN linux-2.4.21/kernel/time.c linux-2.4.21/kernel/time.c
11451--- linux-2.4.21/kernel/time.c 2003-06-23 11:41:31.000000000 -0400
11452+++ linux-2.4.21/kernel/time.c 2003-06-23 11:49:17.000000000 -0400
11453@@ -27,6 +27,7 @@
11454 #include <linux/mm.h>
11455 #include <linux/timex.h>
11456 #include <linux/smp_lock.h>
11457+#include <linux/grsecurity.h>
11458
11459 #include <asm/uaccess.h>
11460
11461@@ -89,6 +90,9 @@
11462 time_maxerror = NTP_PHASE_LIMIT;
11463 time_esterror = NTP_PHASE_LIMIT;
11464 write_unlock_irq(&xtime_lock);
11465+
11466+ gr_log_timechange();
11467+
11468 return 0;
11469 }
11470
11471@@ -167,6 +171,8 @@
11472 * globally block out interrupts when it runs.
11473 */
11474 do_settimeofday(tv);
11475+
11476+ gr_log_timechange();
11477 }
11478 return 0;
11479 }
11480diff -urN linux-2.4.21/kernel/timer.c linux-2.4.21/kernel/timer.c
11481--- linux-2.4.21/kernel/timer.c 2003-06-23 11:41:31.000000000 -0400
11482+++ linux-2.4.21/kernel/timer.c 2003-06-23 11:49:17.000000000 -0400
11483@@ -541,6 +541,9 @@
11484
11485 psecs = (p->times.tms_utime += user);
11486 psecs += (p->times.tms_stime += system);
11487+
11488+ gr_learn_resource(p, RLIMIT_CPU, psecs / HZ);
11489+
11490 if (psecs / HZ > p->rlim[RLIMIT_CPU].rlim_cur) {
11491 /* Send SIGXCPU every second.. */
11492 if (!(psecs % HZ))
11493diff -urN linux-2.4.21/mm/filemap.c linux-2.4.21/mm/filemap.c
11494--- linux-2.4.21/mm/filemap.c 2003-06-23 11:41:31.000000000 -0400
11495+++ linux-2.4.21/mm/filemap.c 2003-06-23 11:49:17.000000000 -0400
11496@@ -2501,6 +2507,7 @@
11497 error = -EIO;
11498 rlim_rss = current->rlim ? current->rlim[RLIMIT_RSS].rlim_cur :
11499 LONG_MAX; /* default: see resource.h */
11500+ gr_learn_resource(current, RLIMIT_RSS, vma->vm_mm->rss + (end - start));
11501 if ((vma->vm_mm->rss + (end - start)) > rlim_rss)
11502 return error;
11503
11504@@ -3004,6 +3011,7 @@
11505 err = -EFBIG;
11506
11507 if (!S_ISBLK(inode->i_mode) && limit != RLIM_INFINITY) {
11508+ gr_learn_resource(current, RLIMIT_FSIZE, pos);
11509 if (pos >= limit) {
11510 send_sig(SIGXFSZ, current, 0);
11511 goto out;
11512@@ -3039,6 +3047,7 @@
11513 */
11514
11515 if (!S_ISBLK(inode->i_mode)) {
11516+ gr_learn_resource(current, RLIMIT_FSIZE, count + (u32)pos);
11517 if (pos >= inode->i_sb->s_maxbytes)
11518 {
11519 if (count || pos > inode->i_sb->s_maxbytes) {
11520diff -urN linux-2.4.21/mm/memory.c linux-2.4.21/mm/memory.c
11521--- linux-2.4.21/mm/memory.c 2003-06-23 11:41:31.000000000 -0400
11522+++ linux-2.4.21/mm/memory.c 2003-06-23 11:49:17.000000000 -0400
11523@@ -1065,6 +1129,7 @@
11524
11525 do_expand:
11526 limit = current->rlim[RLIMIT_FSIZE].rlim_cur;
11527+ gr_learn_resource(current, RLIMIT_FSIZE, offset);
11528 if (limit != RLIM_INFINITY && offset > limit)
11529 goto out_sig;
11530 if (offset > inode->i_sb->s_maxbytes)
11531diff -urN linux-2.4.21/mm/mlock.c linux-2.4.21/mm/mlock.c
11532--- linux-2.4.21/mm/mlock.c 2003-06-23 11:41:31.000000000 -0400
11533+++ linux-2.4.21/mm/mlock.c 2003-06-23 11:49:17.000000000 -0400
11534@@ -209,6 +235,7 @@
11535 lock_limit >>= PAGE_SHIFT;
11536
11537 /* check against resource limits */
11538+ gr_learn_resource(current, RLIMIT_MEMLOCK, locked);
11539 if (locked > lock_limit)
11540 goto out;
11541
11542@@ -276,6 +303,7 @@
11543 lock_limit >>= PAGE_SHIFT;
11544
11545 ret = -ENOMEM;
11546+ gr_learn_resource(current, RLIMIT_MEMLOCK, current->mm->total_vm);
11547 if (current->mm->total_vm > lock_limit)
11548 goto out;
11549
11550diff -urN linux-2.4.21/mm/mmap.c linux-2.4.21/mm/mmap.c
11551--- linux-2.4.21/mm/mmap.c 2003-06-23 11:41:31.000000000 -0400
11552+++ linux-2.4.21/mm/mmap.c 2003-06-23 11:49:17.000000000 -0400
11553@@ -14,6 +14,8 @@
11554 #include <linux/file.h>
11555 #include <linux/fs.h>
11556 #include <linux/personality.h>
11557+#include <linux/random.h>
11558+#include <linux/grsecurity.h>
11559
11560 #include <asm/uaccess.h>
11561 #include <asm/pgalloc.h>
11562@@ -168,6 +170,7 @@
11563
11564 /* Check against rlimit.. */
11565 rlim = current->rlim[RLIMIT_DATA].rlim_cur;
11566+ gr_learn_resource(current, RLIMIT_DATA, brk - mm->start_data);
11567 if (rlim < RLIM_INFINITY && brk - mm->start_data > rlim)
11568 goto out;
11569
11570@@ -432,6 +462,7 @@
11571 if (vm_flags & VM_LOCKED) {
11572 unsigned long locked = mm->locked_vm << PAGE_SHIFT;
11573 locked += len;
11574+ gr_learn_resource(current, RLIMIT_MEMLOCK, locked);
11575 if (locked > current->rlim[RLIMIT_MEMLOCK].rlim_cur)
11576 return -EAGAIN;
11577 }
11578@@ -480,6 +532,9 @@
11579 }
11580 }
11581
11582+ if (!gr_acl_handle_mmap(file, prot))
11583+ return -EACCES;
11584+
11585 /* Clear old maps */
11586 munmap_back:
11587 vma = find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
11588@@ -490,6 +545,7 @@
11589 }
11590
11591 /* Check against address space limit. */
11592+ gr_learn_resource(current, RLIMIT_AS, (mm->total_vm << PAGE_SHIFT) + len);
11593 if ((mm->total_vm << PAGE_SHIFT) + len
11594 > current->rlim[RLIMIT_AS].rlim_cur)
11595 return -ENOMEM;
11596@@ -1047,6 +1255,7 @@
11597 if (mm->def_flags & VM_LOCKED) {
11598 unsigned long locked = mm->locked_vm << PAGE_SHIFT;
11599 locked += len;
11600+ gr_learn_resource(current, RLIMIT_MEMLOCK, locked);
11601 if (locked > current->rlim[RLIMIT_MEMLOCK].rlim_cur)
11602 return -EAGAIN;
11603 }
11604@@ -1063,6 +1272,7 @@
11605 }
11606
11607 /* Check against address space limits *after* clearing old maps... */
11608+ gr_learn_resource(current, RLIMIT_AS, (mm->total_vm << PAGE_SHIFT) + len);
11609 if ((mm->total_vm << PAGE_SHIFT) + len
11610 > current->rlim[RLIMIT_AS].rlim_cur)
11611 return -ENOMEM;
11612diff -urN linux-2.4.21/mm/mprotect.c linux-2.4.21/mm/mprotect.c
11613--- linux-2.4.21/mm/mprotect.c 2003-06-23 11:41:31.000000000 -0400
11614+++ linux-2.4.21/mm/mprotect.c 2003-06-23 11:49:17.000000000 -0400
11615@@ -7,6 +7,7 @@
11616 #include <linux/smp_lock.h>
11617 #include <linux/shm.h>
11618 #include <linux/mman.h>
11619+#include <linux/grsecurity.h>
11620
11621 #include <asm/uaccess.h>
11622 #include <asm/pgalloc.h>
11623@@ -288,6 +392,11 @@
11624 if (!vma || vma->vm_start > start)
11625 goto out;
11626
11627+ if (!gr_acl_handle_mprotect(vma->vm_file, prot)) {
11628+ error = -EACCES;
11629+ goto out;
11630+ }
11631+
11632 for (nstart = start ; ; ) {
11633 unsigned int newflags;
11634 int last = 0;
11635diff -urN linux-2.4.21/mm/mremap.c linux-2.4.21/mm/mremap.c
11636--- linux-2.4.21/mm/mremap.c 2003-06-23 11:41:31.000000000 -0400
11637+++ linux-2.4.21/mm/mremap.c 2003-06-23 11:49:17.000000000 -0400
11638@@ -278,10 +301,13 @@
11639 unsigned long locked = current->mm->locked_vm << PAGE_SHIFT;
11640 locked += new_len - old_len;
11641 ret = -EAGAIN;
11642+ gr_learn_resource(current, RLIMIT_MEMLOCK, locked);
11643 if (locked > current->rlim[RLIMIT_MEMLOCK].rlim_cur)
11644 goto out;
11645 }
11646 ret = -ENOMEM;
11647+
11648+ gr_learn_resource(current, RLIMIT_AS, (current->mm->total_vm << PAGE_SHIFT) + (new_len - old_len));
11649 if ((current->mm->total_vm << PAGE_SHIFT) + (new_len - old_len)
11650 > current->rlim[RLIMIT_AS].rlim_cur)
11651 goto out;
11652diff -urN linux-2.4.21/mm/shmem.c linux-2.4.21/mm/shmem.c
11653--- linux-2.4.21/mm/shmem.c 2003-06-23 11:41:31.000000000 -0400
11654+++ linux-2.4.21/mm/shmem.c 2003-06-23 11:49:17.000000000 -0400
11655@@ -822,6 +822,8 @@
11656 */
11657 err = -EFBIG;
11658 if (limit != RLIM_INFINITY) {
11659+ gr_learn_resource(current, RLIMIT_FSIZE, pos);
11660+
11661 if (pos >= limit) {
11662 send_sig(SIGXFSZ, current, 0);
11663 goto out;
11664diff -urN linux-2.4.21/net/ipv4/af_inet.c linux-2.4.21/net/ipv4/af_inet.c
11665--- linux-2.4.21/net/ipv4/af_inet.c 2003-06-23 11:41:55.000000000 -0400
11666+++ linux-2.4.21/net/ipv4/af_inet.c 2003-06-23 11:49:17.000000000 -0400
11667@@ -83,6 +83,7 @@
11668 #include <linux/init.h>
11669 #include <linux/poll.h>
11670 #include <linux/netfilter_ipv4.h>
11671+#include <linux/grsecurity.h>
11672
11673 #include <asm/uaccess.h>
11674 #include <asm/system.h>
11675@@ -374,7 +375,12 @@
11676 else
11677 sk->protinfo.af_inet.pmtudisc = IP_PMTUDISC_WANT;
11678
11679- sk->protinfo.af_inet.id = 0;
11680+#ifdef CONFIG_GRKERNSEC_RANDID
11681+ if(grsec_enable_randid)
11682+ sk->protinfo.af_inet.id = htons(ip_randomid());
11683+ else
11684+#endif
11685+ sk->protinfo.af_inet.id = 0;
11686
11687 sock_init_data(sock,sk);
11688
11689diff -urN linux-2.4.21/net/ipv4/icmp.c linux-2.4.21/net/ipv4/icmp.c
11690--- linux-2.4.21/net/ipv4/icmp.c 2003-06-23 11:41:55.000000000 -0400
11691+++ linux-2.4.21/net/ipv4/icmp.c 2003-06-23 11:49:17.000000000 -0400
11692@@ -87,6 +87,8 @@
11693 #include <linux/errno.h>
11694 #include <linux/timer.h>
11695 #include <linux/init.h>
11696+#include <linux/grsecurity.h>
11697+
11698 #include <asm/system.h>
11699 #include <asm/uaccess.h>
11700 #include <net/checksum.h>
11701@@ -712,6 +714,12 @@
11702
11703 icmp_param.data.icmph=*skb->h.icmph;
11704 icmp_param.data.icmph.type=ICMP_ECHOREPLY;
11705+
11706+#ifdef CONFIG_GRKERNSEC_RANDPING
11707+ if(grsec_enable_randping)
11708+ icmp_param.data.icmph.un.echo.id = skb->h.icmph->un.echo.id;
11709+#endif
11710+
11711 icmp_param.skb=skb;
11712 icmp_param.offset=0;
11713 icmp_param.data_len=skb->len;
11714diff -urN linux-2.4.21/net/ipv4/ip_output.c linux-2.4.21/net/ipv4/ip_output.c
11715--- linux-2.4.21/net/ipv4/ip_output.c 2003-06-23 11:41:55.000000000 -0400
11716+++ linux-2.4.21/net/ipv4/ip_output.c 2003-06-23 11:49:17.000000000 -0400
11717@@ -77,6 +77,7 @@
11718 #include <linux/netfilter_ipv4.h>
11719 #include <linux/mroute.h>
11720 #include <linux/netlink.h>
11721+#include <linux/grsecurity.h>
11722
11723 /*
11724 * Shall we try to damage output packets if routing dev changes?
11725@@ -511,7 +512,13 @@
11726 * Begin outputting the bytes.
11727 */
11728
11729- id = sk->protinfo.af_inet.id++;
11730+#ifdef CONFIG_GRKERNSEC_RANDID
11731+ if(grsec_enable_randid) {
11732+ id = htons(ip_randomid());
11733+ sk->protinfo.af_inet.id = htons(ip_randomid());
11734+ } else
11735+#endif
11736+ id = sk->protinfo.af_inet.id++;
11737
11738 do {
11739 char *data;
11740diff -urN linux-2.4.21/net/ipv4/netfilter/Config.in linux-2.4.21/net/ipv4/netfilter/Config.in
11741--- linux-2.4.21/net/ipv4/netfilter/Config.in 2003-06-23 11:41:55.000000000 -0400
11742+++ linux-2.4.21/net/ipv4/netfilter/Config.in 2003-06-23 11:49:17.000000000 -0400
11743@@ -32,6 +32,7 @@
11744 dep_tristate ' address type match support' CONFIG_IP_NF_MATCH_ADDRTYPE $CONFIG_IP_NF_IPTABLES
11745 dep_tristate ' tcpmss match support' CONFIG_IP_NF_MATCH_TCPMSS $CONFIG_IP_NF_IPTABLES
11746 dep_tristate ' realm match support' CONFIG_IP_NF_MATCH_REALM $CONFIG_IP_NF_IPTABLES
11747+ dep_tristate ' stealth match support' CONFIG_IP_NF_MATCH_STEALTH $CONFIG_IP_NF_IPTABLES
11748 if [ "$CONFIG_IP_NF_CONNTRACK" != "n" ]; then
11749 dep_tristate ' Helper match support' CONFIG_IP_NF_MATCH_HELPER $CONFIG_IP_NF_IPTABLES
11750 fi
11751diff -urN linux-2.4.21/net/ipv4/netfilter/Makefile linux-2.4.21/net/ipv4/netfilter/Makefile
11752--- linux-2.4.21/net/ipv4/netfilter/Makefile 2003-06-23 11:41:55.000000000 -0400
11753+++ linux-2.4.21/net/ipv4/netfilter/Makefile 2003-06-23 11:49:17.000000000 -0400
11754@@ -83,6 +83,7 @@
11755 obj-$(CONFIG_IP_NF_MATCH_UNCLEAN) += ipt_unclean.o
11756 obj-$(CONFIG_IP_NF_MATCH_STRING) += ipt_string.o
11757 obj-$(CONFIG_IP_NF_MATCH_TCPMSS) += ipt_tcpmss.o
11758+obj-$(CONFIG_IP_NF_MATCH_STEALTH) += ipt_stealth.o
11759 obj-$(CONFIG_IP_NF_MATCH_ADDRTYPE) += ipt_addrtype.o
11760 obj-$(CONFIG_IP_NF_MATCH_REALM) += ipt_realm.o
11761
11762diff -urN linux-2.4.21/net/ipv4/netfilter/ipt_stealth.c linux-2.4.21/net/ipv4/netfilter/ipt_stealth.c
11763--- linux-2.4.21/net/ipv4/netfilter/ipt_stealth.c 1969-12-31 19:00:00.000000000 -0500
11764+++ linux-2.4.21/net/ipv4/netfilter/ipt_stealth.c 2003-06-23 11:49:17.000000000 -0400
11765@@ -0,0 +1,109 @@
11766+/* Kernel module to add stealth support.
11767+ *
11768+ * Copyright (C) 2002 Brad Spengler <spender@grsecurity.net>
11769+ *
11770+ */
11771+
11772+#include <linux/kernel.h>
11773+#include <linux/module.h>
11774+#include <linux/skbuff.h>
11775+#include <linux/net.h>
11776+#include <linux/sched.h>
11777+#include <linux/inet.h>
11778+#include <linux/stddef.h>
11779+
11780+#include <net/ip.h>
11781+#include <net/sock.h>
11782+#include <net/tcp.h>
11783+#include <net/udp.h>
11784+#include <net/route.h>
11785+#include <net/inet_common.h>
11786+
11787+#include <linux/netfilter_ipv4/ip_tables.h>
11788+
11789+MODULE_LICENSE("GPL");
11790+
11791+extern struct sock *udp_v4_lookup(u32 saddr, u16 sport, u32 daddr, u16 dport, int dif);
11792+
11793+static int
11794+match(const struct sk_buff *skb,
11795+ const struct net_device *in,
11796+ const struct net_device *out,
11797+ const void *matchinfo,
11798+ int offset,
11799+ const void *hdr,
11800+ u_int16_t datalen,
11801+ int *hotdrop)
11802+{
11803+ struct iphdr *ip = skb->nh.iph;
11804+ struct tcphdr *th = (struct tcphdr *) hdr;
11805+ struct udphdr *uh = (struct udphdr *) hdr;
11806+ struct sock *sk = NULL;
11807+
11808+ if (!ip || !hdr || offset) return 0;
11809+
11810+ switch(ip->protocol) {
11811+ case IPPROTO_TCP:
11812+ if (datalen < sizeof(struct tcphdr)) {
11813+ *hotdrop = 1;
11814+ return 0;
11815+ }
11816+ if (!(th->syn && !th->ack)) return 0;
11817+ sk = tcp_v4_lookup_listener(ip->daddr, ntohs(th->dest), ((struct rtable*)skb->dst)->rt_iif);
11818+ break;
11819+ case IPPROTO_UDP:
11820+ if (datalen < sizeof(struct udphdr)) {
11821+ *hotdrop = 1;
11822+ return 0;
11823+ }
11824+ sk = udp_v4_lookup(ip->saddr, uh->source, ip->daddr, uh->dest, skb->dev->ifindex);
11825+ break;
11826+ default:
11827+ return 0;
11828+ }
11829+
11830+ if(!sk) // port is being listened on, match this
11831+ return 1;
11832+ else {
11833+ sock_put(sk);
11834+ return 0;
11835+ }
11836+}
11837+
11838+/* Called when user tries to insert an entry of this type. */
11839+static int
11840+checkentry(const char *tablename,
11841+ const struct ipt_ip *ip,
11842+ void *matchinfo,
11843+ unsigned int matchsize,
11844+ unsigned int hook_mask)
11845+{
11846+ if (matchsize != IPT_ALIGN(0))
11847+ return 0;
11848+
11849+ if(((ip->proto == IPPROTO_TCP && !(ip->invflags & IPT_INV_PROTO)) ||
11850+ ((ip->proto == IPPROTO_UDP) && !(ip->invflags & IPT_INV_PROTO)))
11851+ && (hook_mask & (1 << NF_IP_LOCAL_IN)))
11852+ return 1;
11853+
11854+ printk("stealth: Only works on TCP and UDP for the INPUT chain.\n");
11855+
11856+ return 0;
11857+}
11858+
11859+
11860+static struct ipt_match stealth_match
11861+= { { NULL, NULL }, "stealth", &match, &checkentry, NULL, THIS_MODULE };
11862+
11863+static int __init init(void)
11864+{
11865+ return ipt_register_match(&stealth_match);
11866+}
11867+
11868+static void __exit fini(void)
11869+{
11870+ ipt_unregister_match(&stealth_match);
11871+}
11872+
11873+module_init(init);
11874+module_exit(fini);
11875diff -urN linux-2.4.21/net/ipv4/tcp_ipv4.c linux-2.4.21/net/ipv4/tcp_ipv4.c
11876--- linux-2.4.21/net/ipv4/tcp_ipv4.c 2003-06-23 11:41:55.000000000 -0400
11877+++ linux-2.4.21/net/ipv4/tcp_ipv4.c 2003-06-23 11:49:17.000000000 -0400
11878@@ -67,6 +67,7 @@
11879 #include <linux/inet.h>
11880 #include <linux/stddef.h>
11881 #include <linux/ipsec.h>
11882+#include <linux/grsecurity.h>
11883
11884 extern int sysctl_ip_dynaddr;
11885 extern int sysctl_ip_default_ttl;
11886@@ -221,9 +222,18 @@
11887
11888 spin_lock(&tcp_portalloc_lock);
11889 rover = tcp_port_rover;
11890- do { rover++;
11891- if ((rover < low) || (rover > high))
11892- rover = low;
11893+ do {
11894+#ifdef CONFIG_GRKERNSEC_RANDSRC
11895+ if (grsec_enable_randsrc && (high > low)) {
11896+ rover = low + (arc4random() % (high - low));
11897+ } else
11898+#endif
11899+ {
11900+ rover++;
11901+ if ((rover < low) || (rover > high))
11902+ rover = low;
11903+ }
11904+
11905 head = &tcp_bhash[tcp_bhashfn(rover)];
11906 spin_lock(&head->lock);
11907 for (tb = head->chain; tb; tb = tb->next)
11908@@ -546,6 +556,11 @@
11909
11910 static inline __u32 tcp_v4_init_sequence(struct sock *sk, struct sk_buff *skb)
11911 {
11912+#ifdef CONFIG_GRKERNSEC_RANDISN
11913+ if (likely(grsec_enable_randisn))
11914+ return ip_randomisn();
11915+ else
11916+#endif
11917 return secure_tcp_sequence_number(skb->nh.iph->daddr,
11918 skb->nh.iph->saddr,
11919 skb->h.th->dest,
11920@@ -681,9 +696,16 @@
11921 rover = tcp_port_rover;
11922
11923 do {
11924- rover++;
11925- if ((rover < low) || (rover > high))
11926- rover = low;
11927+#ifdef CONFIG_GRKERNSEC_RANDSRC
11928+ if(grsec_enable_randsrc && (high > low)) {
11929+ rover = low + (arc4random() % (high - low));
11930+ } else
11931+#endif
11932+ {
11933+ rover++;
11934+ if ((rover < low) || (rover > high))
11935+ rover = low;
11936+ }
11937 head = &tcp_bhash[tcp_bhashfn(rover)];
11938 spin_lock(&head->lock);
11939
11940@@ -844,11 +866,22 @@
11941 if (err)
11942 goto failure;
11943
11944- if (!tp->write_seq)
11945+ if (!tp->write_seq) {
11946+#ifdef CONFIG_GRKERNSEC_RANDISN
11947+ if (likely(grsec_enable_randisn))
11948+ tp->write_seq = ip_randomisn();
11949+ else
11950+#endif
11951 tp->write_seq = secure_tcp_sequence_number(sk->saddr, sk->daddr,
11952 sk->sport, usin->sin_port);
11953+ }
11954
11955- sk->protinfo.af_inet.id = tp->write_seq^jiffies;
11956+#ifdef CONFIG_GRKERNSEC_RANDID
11957+ if(grsec_enable_randid)
11958+ sk->protinfo.af_inet.id = htons(ip_randomid());
11959+ else
11960+#endif
11961+ sk->protinfo.af_inet.id = tp->write_seq^jiffies;
11962
11963 err = tcp_connect(sk);
11964 if (err)
11965@@ -1570,7 +1603,13 @@
11966 newtp->ext_header_len = 0;
11967 if (newsk->protinfo.af_inet.opt)
11968 newtp->ext_header_len = newsk->protinfo.af_inet.opt->optlen;
11969- newsk->protinfo.af_inet.id = newtp->write_seq^jiffies;
11970+
11971+#ifdef CONFIG_GRKERNSEC_RANDID
11972+ if(grsec_enable_randid)
11973+ newsk->protinfo.af_inet.id = htons(ip_randomid());
11974+ else
11975+#endif
11976+ newsk->protinfo.af_inet.id = newtp->write_seq^jiffies;
11977
11978 tcp_sync_mss(newsk, dst->pmtu);
11979 newtp->advmss = dst->advmss;
11980diff -urN linux-2.4.21/net/ipv4/udp.c linux-2.4.21/net/ipv4/udp.c
11981--- linux-2.4.21/net/ipv4/udp.c 2003-06-23 11:41:55.000000000 -0400
11982+++ linux-2.4.21/net/ipv4/udp.c 2003-06-23 11:49:17.000000000 -0400
11983@@ -91,6 +91,7 @@
11984 #include <net/ipv6.h>
11985 #include <net/protocol.h>
11986 #include <linux/skbuff.h>
11987+#include <linux/grsecurity.h>
11988 #include <net/sock.h>
11989 #include <net/udp.h>
11990 #include <net/icmp.h>
11991@@ -98,6 +99,11 @@
11992 #include <net/inet_common.h>
11993 #include <net/checksum.h>
11994
11995+extern int gr_search_udp_recvmsg(const struct sock *sk,
11996+ const struct sk_buff *skb);
11997+extern int gr_search_udp_sendmsg(const struct sock *sk,
11998+ const struct sockaddr_in *addr);
11999+
12000 /*
12001 * Snmp MIB for the UDP layer
12002 */
12003@@ -474,9 +480,16 @@
12004 ufh.uh.dest = usin->sin_port;
12005 if (ufh.uh.dest == 0)
12006 return -EINVAL;
12007+
12008+ if (!gr_search_udp_sendmsg(sk, usin))
12009+ return -EPERM;
12010 } else {
12011 if (sk->state != TCP_ESTABLISHED)
12012 return -ENOTCONN;
12013+
12014+ if (!gr_search_udp_sendmsg(sk, NULL))
12015+ return -EPERM;
12016+
12017 ufh.daddr = sk->daddr;
12018 ufh.uh.dest = sk->dport;
12019 /* Open fast path for connected socket.
12020@@ -484,6 +497,7 @@
12021 */
12022 connected = 1;
12023 }
12024+
12025 ipc.addr = sk->saddr;
12026 ufh.uh.source = sk->sport;
12027
12028@@ -655,6 +669,11 @@
12029 if (!skb)
12030 goto out;
12031
12032+ if (!gr_search_udp_recvmsg(sk, skb)) {
12033+ err = -EPERM;
12034+ goto out_free;
12035+ }
12036+
12037 copied = skb->len - sizeof(struct udphdr);
12038 if (copied > len) {
12039 copied = len;
12040@@ -759,7 +778,13 @@
12041 sk->daddr = rt->rt_dst;
12042 sk->dport = usin->sin_port;
12043 sk->state = TCP_ESTABLISHED;
12044- sk->protinfo.af_inet.id = jiffies;
12045+
12046+#ifdef CONFIG_GRKERNSEC_RANDID
12047+ if(grsec_enable_randid)
12048+ sk->protinfo.af_inet.id = htons(ip_randomid());
12049+ else
12050+#endif
12051+ sk->protinfo.af_inet.id = jiffies;
12052
12053 sk_dst_set(sk, &rt->u.dst);
12054 return(0);
12055diff -urN linux-2.4.21/net/netlink/af_netlink.c linux-2.4.21/net/netlink/af_netlink.c
12056--- linux-2.4.21/net/netlink/af_netlink.c 2003-06-23 11:41:58.000000000 -0400
12057+++ linux-2.4.21/net/netlink/af_netlink.c 2003-06-23 11:49:17.000000000 -0400
12058@@ -40,6 +40,7 @@
12059 #include <linux/proc_fs.h>
12060 #include <linux/smp_lock.h>
12061 #include <linux/notifier.h>
12062+#include <linux/grsecurity.h>
12063 #include <net/sock.h>
12064 #include <net/scm.h>
12065
12066@@ -620,7 +621,8 @@
12067 check them, when this message will be delivered
12068 to corresponding kernel module. --ANK (980802)
12069 */
12070- NETLINK_CB(skb).eff_cap = current->cap_effective;
12071+
12072+ NETLINK_CB(skb).eff_cap = gr_cap_rtnetlink();
12073
12074 err = -EFAULT;
12075 if (memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len)) {
12076diff -urN linux-2.4.21/net/netsyms.c linux-2.4.21/net/netsyms.c
12077--- linux-2.4.21/net/netsyms.c 2003-06-23 11:41:53.000000000 -0400
12078+++ linux-2.4.21/net/netsyms.c 2003-06-23 11:49:17.000000000 -0400
12079@@ -24,6 +24,7 @@
12080 #include <net/checksum.h>
12081 #include <linux/etherdevice.h>
12082 #include <net/route.h>
12083+#include <linux/grsecurity.h>
12084 #ifdef CONFIG_HIPPI
12085 #include <linux/hippidevice.h>
12086 #endif
12087@@ -598,6 +599,47 @@
12088
12089 EXPORT_SYMBOL(softnet_data);
12090
12091+#if defined(CONFIG_IP_NF_MATCH_STEALTH_MODULE)
12092+#if !defined (CONFIG_IPV6_MODULE) && !defined (CONFIG_KHTTPD) && !defined (CONFIG_KHTTPD_MODULE)
12093+EXPORT_SYMBOL(tcp_v4_lookup_listener);
12094+#endif
12095+#endif
12096+
12097+#if defined(CONFIG_GRKERNSEC_RANDID)
12098+EXPORT_SYMBOL(ip_randomid);
12099+#endif
12100+#if defined(CONFIG_GRKERNSEC_RANDSRC) || defined(CONFIG_GRKERNSEC_RANDRPC)
12101+EXPORT_SYMBOL(arc4random);
12102+#endif
12103+#ifdef CONFIG_GRKERNSEC_RANDISN
12104+EXPORT_SYMBOL(ip_randomisn);
12105+EXPORT_SYMBOL(grsec_enable_randisn);
12106+#endif
12107+#ifdef CONFIG_GRKERNSEC_RANDID
12108+EXPORT_SYMBOL(grsec_enable_randid);
12109+#endif
12110+#ifdef CONFIG_GRKERNSEC_RANDSRC
12111+EXPORT_SYMBOL(grsec_enable_randsrc);
12112+#endif
12113+#ifdef CONFIG_GRKERNSEC_RANDRPC
12114+EXPORT_SYMBOL(grsec_enable_randrpc);
12115+#endif
12116+
12117+EXPORT_SYMBOL(gr_cap_rtnetlink);
12118+
12119+extern int gr_search_udp_recvmsg(const struct sock *sk, const struct sk_buff *skb);
12120+extern int gr_search_udp_sendmsg(const struct sock *sk, const struct sockaddr_in *addr);
12121+
12122+EXPORT_SYMBOL(gr_search_udp_recvmsg);
12123+EXPORT_SYMBOL(gr_search_udp_sendmsg);
12124+
12125+#ifdef CONFIG_UNIX_MODULE
12126+EXPORT_SYMBOL(gr_acl_handle_unix);
12127+EXPORT_SYMBOL(gr_acl_handle_mknod);
12128+EXPORT_SYMBOL(gr_handle_chroot_unix);
12129+EXPORT_SYMBOL(gr_handle_create);
12130+#endif
12131+
12132 #if defined(CONFIG_NET_RADIO) || defined(CONFIG_NET_PCMCIA_RADIO)
12133 #include <net/iw_handler.h>
12134 EXPORT_SYMBOL(wireless_send_event);
12135diff -urN linux-2.4.21/net/socket.c linux-2.4.21/net/socket.c
12136--- linux-2.4.21/net/socket.c 2003-06-23 11:41:53.000000000 -0400
12137+++ linux-2.4.21/net/socket.c 2003-06-23 11:49:17.000000000 -0400
12138@@ -85,6 +85,18 @@
12139 #include <net/scm.h>
12140 #include <linux/netfilter.h>
12141
12142+extern void gr_attach_curr_ip(const struct sock *sk);
12143+extern int gr_handle_sock_all(const int family, const int type,
12144+ const int protocol);
12145+extern int gr_handle_sock_server(const struct sockaddr *sck);
12146+extern int gr_handle_sock_client(const struct sockaddr *sck);
12147+extern int gr_search_connect(const struct socket * sock,
12148+ const struct sockaddr_in * addr);
12149+extern int gr_search_bind(const struct socket * sock,
12150+ const struct sockaddr_in * addr);
12151+extern int gr_search_socket(const int domain, const int type,
12152+ const int protocol);
12153+
12154 static int sock_no_open(struct inode *irrelevant, struct file *dontcare);
12155 static ssize_t sock_read(struct file *file, char *buf,
12156 size_t size, loff_t *ppos);
12157@@ -699,6 +711,7 @@
12158
12159 int sock_close(struct inode *inode, struct file *filp)
12160 {
12161+ struct socket *sock;
12162 /*
12163 * It was possible the inode is NULL we were
12164 * closing an unfinished socket.
12165@@ -709,8 +722,21 @@
12166 printk(KERN_DEBUG "sock_close: NULL inode\n");
12167 return 0;
12168 }
12169+ sock = socki_lookup(inode);
12170+
12171 sock_fasync(-1, filp, 0);
12172+
12173+#ifdef CONFIG_GRKERNSEC
12174+ if (unlikely(current->used_accept && sock->sk &&
12175+ (sock->sk->protocol == IPPROTO_TCP) &&
12176+ (sock->sk->daddr == current->curr_ip))) {
12177+ current->used_accept = 0;
12178+ current->curr_ip = 0;
12179+ }
12180+#endif
12181+
12182 sock_release(socki_lookup(inode));
12183+
12184 return 0;
12185 }
12186
12187@@ -903,6 +929,16 @@
12188 int retval;
12189 struct socket *sock;
12190
12191+ if(!gr_search_socket(family, type, protocol)) {
12192+ retval = -EACCES;
12193+ goto out;
12194+ }
12195+
12196+ if (gr_handle_sock_all(family, type, protocol)) {
12197+ retval = -EACCES;
12198+ goto out;
12199+ }
12200+
12201 retval = sock_create(family, type, protocol, &sock);
12202 if (retval < 0)
12203 goto out;
12204@@ -998,12 +1034,26 @@
12205 {
12206 struct socket *sock;
12207 char address[MAX_SOCK_ADDR];
12208+ struct sockaddr * sck;
12209 int err;
12210
12211 if((sock = sockfd_lookup(fd,&err))!=NULL)
12212 {
12213- if((err=move_addr_to_kernel(umyaddr,addrlen,address))>=0)
12214+ if((err=move_addr_to_kernel(umyaddr,addrlen,address))>=0) {
12215+ sck = (struct sockaddr *) address;
12216+
12217+ if(!gr_search_bind(sock, (struct sockaddr_in *) sck)) {
12218+ sockfd_put(sock);
12219+ return -EACCES;
12220+ }
12221+
12222+ if (gr_handle_sock_server(sck)) {
12223+ sockfd_put(sock);
12224+ return -EACCES;
12225+ }
12226+
12227 err = sock->ops->bind(sock, (struct sockaddr *)address, addrlen);
12228+ }
12229 sockfd_put(sock);
12230 }
12231 return err;
12232@@ -1079,6 +1129,8 @@
12233 if ((err = sock_map_fd(newsock)) < 0)
12234 goto out_release;
12235
12236+ gr_attach_curr_ip(newsock->sk);
12237+
12238 out_put:
12239 sockfd_put(sock);
12240 out:
12241@@ -1106,6 +1158,7 @@
12242 {
12243 struct socket *sock;
12244 char address[MAX_SOCK_ADDR];
12245+ struct sockaddr * sck;
12246 int err;
12247
12248 sock = sockfd_lookup(fd, &err);
12249@@ -1114,6 +1167,24 @@
12250 err = move_addr_to_kernel(uservaddr, addrlen, address);
12251 if (err < 0)
12252 goto out_put;
12253+
12254+ sck = (struct sockaddr *) address;
12255+
12256+ if (!gr_search_connect(sock, (struct sockaddr_in *) sck)) {
12257+ err = -EACCES;
12258+ goto out_put;
12259+ }
12260+
12261+ if (gr_handle_sock_client(sck)) {
12262+ err = -EACCES;
12263+ goto out_put;
12264+ }
12265+
12266+#ifdef CONFIG_GRKERNSEC
12267+ if (sock->sk->protocol == IPPROTO_TCP)
12268+ current->used_connect = 1;
12269+#endif
12270+
12271 err = sock->ops->connect(sock, (struct sockaddr *) address, addrlen,
12272 sock->file->f_flags);
12273 out_put:
12274@@ -1333,6 +1404,14 @@
12275 err=sock->ops->shutdown(sock, how);
12276 sockfd_put(sock);
12277 }
12278+
12279+#ifdef CONFIG_GRKERNSEC
12280+ if (likely(!err && current->used_accept)) {
12281+ current->used_accept = 0;
12282+ current->curr_ip = 0;
12283+ }
12284+#endif
12285+
12286 return err;
12287 }
12288
12289diff -urN linux-2.4.21/net/sunrpc/xprt.c linux-2.4.21/net/sunrpc/xprt.c
12290--- linux-2.4.21/net/sunrpc/xprt.c 2003-06-23 11:41:57.000000000 -0400
12291+++ linux-2.4.21/net/sunrpc/xprt.c 2003-06-23 11:49:17.000000000 -0400
12292@@ -59,6 +59,7 @@
12293 #include <linux/unistd.h>
12294 #include <linux/sunrpc/clnt.h>
12295 #include <linux/file.h>
12296+#include <linux/grsecurity.h>
12297
12298 #include <net/sock.h>
12299 #include <net/checksum.h>
12300@@ -1267,6 +1268,12 @@
12301 }
12302 ret = xid++;
12303 spin_unlock(&xid_lock);
12304+
12305+#ifdef CONFIG_GRKERNSEC_RANDRPC
12306+ if (grsec_enable_randrpc)
12307+ ret = arc4random();
12308+#endif
12309+
12310 return ret;
12311 }
12312
12313diff -urN linux-2.4.21/net/unix/af_unix.c linux-2.4.21/net/unix/af_unix.c
12314--- linux-2.4.21/net/unix/af_unix.c 2003-06-23 11:41:53.000000000 -0400
12315+++ linux-2.4.21/net/unix/af_unix.c 2003-06-23 11:49:18.000000000 -0400
12316@@ -109,6 +109,7 @@
12317 #include <linux/poll.h>
12318 #include <linux/smp_lock.h>
12319 #include <linux/rtnetlink.h>
12320+#include <linux/grsecurity.h>
12321
12322 #include <asm/checksum.h>
12323
12324@@ -599,6 +600,11 @@
12325 if (err)
12326 goto put_fail;
12327
12328+ if (!gr_acl_handle_unix(nd.dentry, nd.mnt)) {
12329+ err = -EACCES;
12330+ goto put_fail;
12331+ }
12332+
12333 err = -ECONNREFUSED;
12334 if (!S_ISSOCK(nd.dentry->d_inode->i_mode))
12335 goto put_fail;
12336@@ -622,6 +628,13 @@
12337 if (u) {
12338 struct dentry *dentry;
12339 dentry = u->protinfo.af_unix.dentry;
12340+
12341+ if (!gr_handle_chroot_unix(u->peercred.pid)) {
12342+ err = -EPERM;
12343+ sock_put(u);
12344+ goto fail;
12345+ }
12346+
12347 if (dentry)
12348 UPDATE_ATIME(dentry->d_inode);
12349 } else
12350@@ -720,9 +733,19 @@
12351 * All right, let's create it.
12352 */
12353 mode = S_IFSOCK | (sock->inode->i_mode & ~current->fs->umask);
12354+
12355+ if (!gr_acl_handle_mknod(dentry, nd.dentry, nd.mnt, mode)) {
12356+ err = -EACCES;
12357+ goto out_mknod_dput;
12358+ }
12359+
12360 err = vfs_mknod(nd.dentry->d_inode, dentry, mode, 0);
12361+
12362 if (err)
12363 goto out_mknod_dput;
12364+
12365+ gr_handle_create(dentry, nd.mnt);
12366+
12367 up(&nd.dentry->d_inode->i_sem);
12368 dput(nd.dentry);
12369 nd.dentry = dentry;
12370@@ -740,6 +763,10 @@
12371 goto out_unlock;
12372 }
12373
12374+#ifdef CONFIG_GRKERNSEC_CHROOT_UNIX
12375+ sk->peercred.pid = current->pid;
12376+#endif
12377+
12378 list = &unix_socket_table[addr->hash];
12379 } else {
12380 list = &unix_socket_table[dentry->d_inode->i_ino & (UNIX_HASH_SIZE-1)];
12381@@ -866,6 +893,9 @@
12382 int st;
12383 int err;
12384 long timeo;
12385+#ifdef CONFIG_GRKERNSEC
12386+ struct task_struct *p, **htable;
12387+#endif
12388
12389 err = unix_mkname(sunaddr, addr_len, &hash);
12390 if (err < 0)
12391@@ -989,6 +1019,17 @@
12392 /* Set credentials */
12393 sk->peercred = other->peercred;
12394
12395+#ifdef CONFIG_GRKERNSEC
12396+ read_lock(&tasklist_lock);
12397+ htable = &pidhash[pid_hashfn(other->peercred.pid)];
12398+ for (p = *htable; p && p->pid != other->peercred.pid; p = p->pidhash_next);
12399+ if (p) {
12400+ p->curr_ip = current->curr_ip;
12401+ p->used_accept = 1;
12402+ }
12403+ read_unlock(&tasklist_lock);
12404+#endif
12405+
12406 sock_hold(newsk);
12407 unix_peer(sk)=newsk;
12408 sock->state=SS_CONNECTED;
This page took 6.006823 seconds and 4 git commands to generate.