From: Artiom Morozov <artiom@phreaker.net>
To: linux-kernel@vger.kernel.org
Cc: Kiretchko Serguei <spk@csp.org.by>
Subject: select() call corrupts stack
Date: Wed, 27 Feb 2002 21:40:56 +0200 [thread overview]
Message-ID: <20020227214056.A6740@cyan.csp.org.by> (raw)
[-- Attachment #1: Type: text/plain, Size: 626 bytes --]
Hello,
Here's a sample program. Try running it and open about 2k of
connections to port 5222 (you'll need ulimit -n 10000 or like that). It
will segfault. Simple asm like this
__asm__(
"pushl %eax \n\t" "movl 0(%ebp), %eax \n\t"
"cmp $65535, %eax \n\t"
"ja isok \n\t"
"xor %eax, %eax \n\t"
"movl %eax, 0(%eax) \n\t"
"isok: \n\t"
"popl %eax \n\t"
);
after each subroutine call will show you that after select() [ebp] have
weird value. While this is unlikely to be a security flaw, i think this
is a bug.
ps: it's okay for 1k of connections or so
pps: kernel 2.4.17 on i686, gcc 3.0.3, glibc 2.2.3.
[-- Attachment #2: main.cpp --]
[-- Type: text/x-c++, Size: 2789 bytes --]
#include <deque>
#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <netinet/in.h>
using namespace std;
typedef void* (*DISP_ENTRY_ROUTINE)(void*);
typedef struct
{
pthread_mutex_t sockLock;
int sock;
DISP_ENTRY_ROUTINE disp;
} ReqInfo;
pthread_mutex_t reqsLock = PTHREAD_MUTEX_INITIALIZER;
deque<ReqInfo*> reqs;
void* disp0(void*);
void add_to_queue(int s)
{
pthread_mutex_lock(&reqsLock);
ReqInfo * nInfo = new ReqInfo[1];
nInfo->sock = s;
pthread_mutex_init(&nInfo->sockLock, NULL);
nInfo->disp = disp0;
reqs.push_back(nInfo);
pthread_mutex_unlock(&reqsLock);
}
void* disp0(void* param)
{
ReqInfo *p = (ReqInfo *) param;
char foo[0x10];
recv(p->sock, foo, 4, 0);
add_to_queue(p->sock);
return NULL;
}
void* proc_acc(void* param)
{
int s = socket(PF_INET, SOCK_STREAM, 0);
if (s == -1)
{
fprintf(stderr, "proc_acc: socket() failed, %s\n", strerror(errno));
return NULL;
}
struct sockaddr_in saL;
saL.sin_family = AF_INET;
saL.sin_port = htons(5222);
saL.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(s, (sockaddr*)&saL, sizeof(saL)) == -1)
{
fprintf(stderr, "proc_acc: bind() failed, %s\n", strerror(errno));
return NULL;
}
if (listen(s, SOMAXCONN) == -1)
{
fprintf(stderr, "proc_acc: listen() failed, %s\n", strerror(errno));
return NULL;
}
do
{
int len;
struct sockaddr_in addr;
int ch = accept(s, (struct sockaddr*) &addr, (socklen_t*) &len);
if (ch == -1)
{
fprintf(stderr, "proc_acc: accept() failed, %s\n", strerror(errno));
}
else
{
add_to_queue(ch);
}
} while (1);
return NULL;
}
int try_select(ReqInfo *p)
{
fd_set set;
struct timeval tv;
int out = 0;
do
{
tv.tv_sec = tv.tv_usec = 0;
FD_ZERO(&set);
FD_SET(p->sock, &set);
} while ((out = select(p->sock + 1, &set, NULL, NULL, &tv)) == -1
&& errno == EINTR);
return out;
}
void* proc_sel(void* param)
{
do
{
ReqInfo *p = NULL;
pthread_mutex_lock(&reqsLock);
if (reqs.size())
{
p = reqs.front();
reqs.pop_front();
}
pthread_mutex_unlock(&reqsLock);
if (p != NULL)
{
int r = try_select(p);
switch (r)
{
case 0:
{
pthread_mutex_lock(&reqsLock);
reqs.push_back(p);
pthread_mutex_unlock(&reqsLock);
}; break;
case -1:
{
fprintf(stderr, "select(): %s\n", strerror(errno));
};
default:
{
p->disp(p);
delete[] p;
};
}
}
} while (1);
}
int main()
{
pthread_t th_acc, th_sel;
pthread_create(&th_acc, NULL, proc_acc, NULL);
pthread_create(&th_sel, NULL, proc_sel, NULL);
// should crash here, so don't care about cleanup
pthread_join(th_acc, NULL);
return 0;
}
[-- Attachment #3: Makefile --]
[-- Type: text/x-makefile, Size: 53 bytes --]
all:
c++ -pthread -g -Wall -o test-acceptor main.cpp
next reply other threads:[~2002-02-27 19:38 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-02-27 19:40 Artiom Morozov [this message]
2002-02-27 20:19 ` Brian Gerst
2002-02-27 20:28 ` Andreas Schwab
2002-02-27 21:08 ` Jakob Østergaard
2002-02-28 16:33 ` Byron Stanoszek
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20020227214056.A6740@cyan.csp.org.by \
--to=artiom@phreaker.net \
--cc=linux-kernel@vger.kernel.org \
--cc=spk@csp.org.by \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®