📄 00000010.htm
字号:
<HTML><HEAD> <TITLE>BBS水木清华站∶精华区</TITLE></HEAD><BODY><CENTER><H1>BBS水木清华站∶精华区</H1></CENTER>发信人: lenx (冷·枫), 信区: Linux <BR>标 题: [安全] Non-executable user stack and symlink fix <BR>发信站: BBS 水木清华站 (Mon Nov 24 22:01:15 1997) <BR> <BR>Linux目前安全的最佳方案, 对stack overflow和symlink的攻击都有了相当好的防范 <BR> <BR><A HREF="ftp://ftp.sepc.ac.cn/pub/linux/collect/system/kernel/linux-stack-symlink.tgz">ftp://ftp.sepc.ac.cn/pub/linux/collect/system/kernel/linux-stack-symlink.tgz</A> <BR> <BR>(sepc只对166.111/159.226/162.105开放) <BR> <BR> <BR> Non-executable user stack area and symlink fix -- Linux kernel patch <BR> ---------------------------------------------------------------------- <BR> <BR>========== <BR> Overview <BR>========== <BR> <BR>This patch is intended to add protection against two classes of security <BR>holes: buffer overflows and symlinks in /tmp. <BR> <BR>Most buffer overflow exploits are based on overwriting a function's return <BR>address on the stack to point to some arbitrary code, which is also put <BR>onto the stack. If the stack area is non-executable, buffer overflow <BR>vulnerabilities become harder to exploit. <BR> <BR>Another way to exploit a buffer overflow is to point the return address to <BR>a function in libc, usually system(). This patch also changes the default <BR>address that shared libraries are mmap()ed at to make it always contain a <BR>zero byte. This makes it impossible to specify any more data (parameters <BR>to the function, or more copies of the return address when filling with a <BR>pattern) in an exploit that has to do with ASCIIZ strings (this is the <BR>case for most overflow vulnerabilities). <BR> <BR>However, note that this patch is by no means a complete solution, it just <BR>adds an extra layer of security. Some buffer overflow vulnerabilities will <BR>still remain exploitable a more complicated way. The reason for using such <BR>a patch is to protect against some of the buffer overflow vulnerabilities <BR>that are yet unknown. <BR> <BR>In this version of my patch I also added a symlink security fix, originally <BR>by Andrew Tridgell. I changed it to prevent from using hard links too, by <BR>simply not allowing non-root users to create hard links to files they don't <BR>own, in +t directories. This seems to be the desired behavior anyway, since <BR>otherwise users couldn't remove such links they just created. I also added <BR>exploit attempt logging, this code is shared with the non-executable stack <BR>stuff, and was the reason to make it a single patch instead of two separate <BR>ones. You can enable them separately anyway. <BR> <BR>================ <BR> How to install <BR>================ <BR> <BR>Apply the patch. Enable prompting for experimental code in your kernel <BR>configuration, enable the patch itself (in General setup section). Read <BR>help for the suboptions, and configure them. Also, enable the symlink fix <BR>(in Filesystems section). Build the kernel and reboot. <BR> <BR>You may also want to add the following line to your /etc/syslog.conf to <BR>log [security] alerts separately: <BR> <BR>kern.alert /var/log/alert <BR> <BR>Additionally, you may do something like this (assuming the log file will <BR>be empty most of the time): <BR> <BR>><I> /var/log/alert </I><BR>chown root.staff /var/log/alert <BR>chmod 640 /var/log/alert <BR>echo "more /var/log/alert" >> ~your_usual_non-root_account/.bash_profile <BR>chattr +a /var/log/alert <BR> <BR>[ The last command doesn't do much -- there're too many ways to get around <BR>securelevel in Linux right now: loading a kernel module, writing to the <BR>hard disk device, using iopl() and writing to the hard disk via ports... <BR>This can't be done over NFS however... ] <BR> <BR>Ensure that the patch is working correctly, use stacktest.c for that -- <BR>running './stacktest -e' should segfault, and a message about possible <BR>buffer overflow exploit attempt should get logged to /var/log/alert (with <BR>syslogd configuration described above, and if you have logging enabled). <BR>If you enabled GCC trampolines autodetection, try running './stacktest -t', <BR>it should succeed. <BR> <BR>Also, check the address libc is mmap()ed at, its MSB should be zero instead <BR>of 0x40 like it was before. Use a command like this: <BR> <BR>strace /bin/ls 2>&1 | grep mmap\[^\|\]\*\|PROT_EXEC | sed s/\[^=\]\*=// <BR> <BR>If you enabled the symlink fix you can also try to create a symlink in /tmp <BR>(as a non-root user) pointing to a file that user has no read access to, <BR>then switch to some other user that has the read access (for example, root) <BR>and try to read the file via the link (like, cat /tmp/link). This should <BR>fail, and a message should get logged (if enabled). Everything is similar <BR>for write access, and for symlinks to files that don't exist. Now, you can <BR>try to create a hard link in /tmp as a non-root user to a file that user <BR>doesn't own. This should also fail. <BR> <BR>======== <BR> F.A.Q. <BR>======== <BR> <BR>Q: Will GCC-compiled programs that use trampolines work with the patch? <BR>A: Yes, read help for the 'Autodetect GCC trampolines' configuration option. <BR> <BR>Q: How do you differ a trampoline call from an exploit attempt? <BR>A: Since most buffer overflow exploits overwrite the return address, the <BR>instruction to pass control to the stack has to be a RET. With trampoline <BR>calls the instruction is a CALL. However, in some cases such trampoline <BR>autodetection can be fooled by RET'ing to a CALL instruction and making <BR>this CALL pass control onto the stack (in reality, this also requires a <BR>register to be set to the address). Again, read help for the 'Autodetect <BR>GCC trampolines' configuration option. <BR> <BR>Q: What is chstk.c for? <BR>A: The patch adds an extra flag to ELF and a.out headers, which controls <BR>whether the program will be allowed to execute code on the stack or not, <BR>and chstk.c is what you should use to manage the flag. You might find it <BR>useful if you choose to disable the GCC trampolines autodetection. BTW, <BR>setting the flag also restores the original address shared libraries are <BR>mmap()ed at, just in case some program depends on that. <BR> <BR>Q: Why did you modify signal handler return code? <BR>A: Originally the kernel put some code onto the stack to return from signal <BR>handlers. Now signal handler returns are done via the GPF handler instead <BR>(an invalid magic return address is put on the stack). <BR> <BR>Q: What to do if a program needs to follow a symlink in a +t directory for <BR>its normal operation (without introducing a security hole)? <BR>A: Usually such a link needs to be created only once, so create it as root. <BR>Such links are followed even when the patch is enabled. <BR> <BR>Q: What will happen if someone does: <BR>ln -s /etc/passwd ~/link <BR>ln -s ~/link /tmp/link <BR>and the vulnerable program runs as root and writes to /tmp/link? <BR>A: The patch is not looking at the target of the symlink in /tmp, it only <BR>checks if the symlink itself is owned by the user that vulnerable program <BR>is running as, and doesn't follow the link if not (like in this example). <BR> <BR>Q: Is there some performance impact of using the patch? <BR>A: Well, the only thing affected is singal handler returns. I didn't want <BR>to modify the sigreturn syscall, so there is some extra code to setup its <BR>stack frame. I don't think this has a noticable effect on the performance: <BR>saved context checks and other signal handling stuff are taking much more <BR>time. Also, executing code on the stack was not fast anyway. <BR> <BR>Signed, <BR>Solar Designer <<A HREF="mailto:solar@false.com>">solar@false.com></A> <BR> <BR> <BR> <BR>-- <BR>※ 来源:·BBS 水木清华站 bbs.net.tsinghua.edu.cn·[FROM: 162.105.118.33] <BR><CENTER><H1>BBS水木清华站∶精华区</H1></CENTER></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -