public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: bert hubert <ahu@ds9a.nl>
To: Davide Libenzi <davidel@xmailserver.org>
Cc: Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
	lse-tech@lists.sourceforge.net
Subject: Re: and nicer too - Re: [PATCH] epoll more scalable than poll
Date: Tue, 29 Oct 2002 01:40:34 +0100	[thread overview]
Message-ID: <20021029004034.GA32118@outpost.ds9a.nl> (raw)
In-Reply-To: <Pine.LNX.4.44.0210281631040.966-100000@blue1.dev.mcafeelabs.com>

On Mon, Oct 28, 2002 at 04:32:50PM -0800, Davide Libenzi wrote:

> The code above it's just fine. The "fd" is not lost because the falling
> through :
> 
> do_use_fd(fd);
> 
> will make good use of it.

If that code dares to read immediatly from the fd without having an explicit
POLLIN event, which also means that epoll can only be used in this fashion
with nonblocking sockets. 

The listening socket needs to do that anyhow as the connection may have
vanished anyhow - select has that problem too.

To trigger the problem, see the following very nasty 'exploit'. Telnet to
port 10000 and immediately enter a line of text and press enter. This line
might very well be 'GET / HTTP/1.0'. 

Because of the inserted sleep(2); below, this line will never be reported
by sys_epoll_wait() in its current form. To see it appear, enter an
additional line after a while.

So either fix up sys_epoll_ctl() to insert an edge if there is data at
insertion time (which needs some locking probably to get it right), OR
document the interface that it should only be used with non-blocking sockets
and that the caller should immediately try to read after the initial
sys_epoll_ctl insert call. This last solution sounds very bad and confusing.

Code:

#include <stdio.h>
#include <errno.h>
#include <asm/page.h> 
#include <asm/poll.h> 
#include <linux/linkage.h>
#include <linux/eventpoll.h>
#include <linux/unistd.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>

#define __sys_epoll_create(maxfds) _syscall1(int, sys_epoll_create, int, maxfds)
#define __sys_epoll_ctl(epfd, op, fd, events) _syscall4(int, sys_epoll_ctl, \
int, epfd, int, op, int, fd, unsigned int, events)

#define __sys_epoll_wait(epfd, events, timeout) _syscall3(int, sys_epoll_wait, \
int, epfd, struct pollfd const **, events, int, timeout)

__sys_epoll_create(maxfds)
__sys_epoll_ctl(epfd, op, fd, events)
__sys_epoll_wait(epfd, events, timeout)

// asmlinkage int sys_epoll_wait(int epfd, struct pollfd const **events, int timeout);

int main()
{
	int kdpfd;
	struct pollfd const *pfds;
	int nfds;
	int timeout=2;
	struct sockaddr_in local;
	socklen_t addrlen;
	int s, client;
	int n;
	char line[80];
	int ret;

	if ((kdpfd = sys_epoll_create(10)) < 0) {
        	perror("sys_epoll_create");
                return -1;
        }

	s=socket(AF_INET,SOCK_STREAM,0); 

	memset(&local,0,sizeof(local));
        local.sin_family=AF_INET;
	local.sin_addr.s_addr = INADDR_ANY;
	
	int tmp=1;
	if(setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(char*)&tmp,sizeof tmp)<0) {
	  perror("setsockopt");
	  return 0;
	}
	
	local.sin_port=htons(10000);
	
	if(bind(s, (struct sockaddr*)&local,sizeof(local))<0) {
	  perror("bind");
	  return 0;
	}
	
	if(listen(s,128)<0) {
	  perror("listen");
	  return 0;
	}

        if (sys_epoll_ctl(kdpfd, EP_CTL_ADD, s, POLLIN ) < 0) {
		fprintf(stderr, "sys_epoll set insertion error: fd=%d\n", s);

		return -1;
	}                           
	addrlen=sizeof(local);


	for(;;) {
	  nfds = sys_epoll_wait(kdpfd, &pfds, -1);	
	  fprintf(stderr,"sys_epoll_wait returned: %d\n",nfds);
	  
	  for(n=0;n<nfds;++n) {
	    if(pfds[n].fd==s) {
	      client=accept(s, (struct sockaddr*)&local, &addrlen);
	      sleep(2);      
	      if(client<0){
		perror("accept");
		continue;
	      }
	      if (sys_epoll_ctl(kdpfd, EP_CTL_ADD, client, POLLIN ) < 0) {
		fprintf(stderr, "sys_epoll set insertion error: fd=%d\n", client);
		return -1;
	      }                                        
	    }
	    else {
	      if((ret=read(pfds[n].fd,line,79))>0)
		printf("read from fd %d: '%.*s'\n", pfds[n].fd, ret>2 ? ret-2 : 0,line);
	    }
	  }

	  

	}
	return 0;
}



> 
> 
> 
> - Davide
> 
> 
> -
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at  http://www.tux.org/lkml/

-- 
http://www.PowerDNS.com          Versatile DNS Software & Services
http://lartc.org           Linux Advanced Routing & Traffic Control HOWTO

  reply	other threads:[~2002-10-29  0:34 UTC|newest]

Thread overview: 117+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-10-28 19:14 [PATCH] epoll more scalable than poll Hanna Linder
2002-10-28 20:10 ` Hanna Linder
2002-10-28 20:56 ` Martin Waitz
2002-10-28 22:02   ` bert hubert
2002-10-28 22:15     ` bert hubert
2002-10-28 22:17   ` Davide Libenzi
2002-10-28 22:08 ` bert hubert
2002-10-28 22:12   ` [Lse-tech] " Shailabh Nagar
2002-10-28 22:37     ` Davide Libenzi
2002-10-28 22:29   ` Davide Libenzi
2002-10-28 22:58     ` and nicer too - " bert hubert
2002-10-28 23:23       ` Davide Libenzi
2002-10-28 23:44     ` Jamie Lokier
2002-10-29  0:02       ` Davide Libenzi
2002-10-29  1:51         ` Jamie Lokier
2002-10-29  5:06           ` Davide Libenzi
2002-10-29 11:20             ` Jamie Lokier
2002-10-30  0:16               ` Davide Libenzi
2002-10-29  0:03       ` bert hubert
2002-10-29  0:20         ` Davide Libenzi
2002-10-29  0:48         ` Jamie Lokier
2002-10-29  1:53           ` Jamie Lokier
2002-10-28 23:45   ` and nicer too - " John Gardiner Myers
2002-10-29  0:08     ` Davide Libenzi
2002-10-29 12:59       ` Martin Waitz
2002-10-29 15:19         ` bert hubert
2002-10-29 22:54           ` Martin Waitz
2002-10-30  2:24             ` Davide Libenzi
2002-10-30 19:38               ` Martin Waitz
2002-10-31  5:04                 ` Davide Libenzi
2002-10-29  0:18     ` bert hubert
2002-10-29  0:32       ` Davide Libenzi
2002-10-29  0:40         ` bert hubert [this message]
2002-10-29  0:57           ` Davide Libenzi
2002-10-29  0:53             ` bert hubert
2002-10-29  1:13               ` Davide Libenzi
2002-10-29  1:08                 ` [Lse-tech] " Hanna Linder
2002-10-29  1:39                   ` Davide Libenzi
2002-10-29  2:05               ` Jamie Lokier
2002-10-29  2:44                 ` Davide Libenzi
2002-10-29  4:01                   ` [PATCH] Updated sys_epoll now with man pages Hanna Linder
2002-10-29  5:09                     ` Andrew Morton
2002-10-29  5:28                       ` [Lse-tech] " Randy.Dunlap
2002-10-29  5:47                         ` Davide Libenzi
2002-10-29  5:41                           ` Randy.Dunlap
2002-10-29  6:12                             ` Davide Libenzi
2002-10-29  6:03                               ` Randy.Dunlap
2002-10-29  6:23                                 ` Davide Libenzi
2002-10-29 14:59                         ` Paul Larson
2002-10-29  5:31                       ` Davide Libenzi
2002-10-29  7:34                       ` Davide Libenzi
2002-10-29 11:04                       ` bert hubert
2002-10-29 15:30                       ` [Lse-tech] " Shailabh Nagar
2002-10-29 17:45                         ` Davide Libenzi
2002-10-29 19:30                           ` Hanna Linder
2002-10-29 19:49                             ` Davide Libenzi
2002-10-29 13:09             ` and nicer too - Re: [PATCH] epoll more scalable than poll bert hubert
2002-10-29 21:25               ` Davide Libenzi
2002-10-29 21:23                 ` Hanna Linder
2002-10-29 21:41                   ` Davide Libenzi
2002-10-29 23:06                     ` Hanna Linder
2002-10-29 23:14                       ` [Lse-tech] " Randy.Dunlap
2002-10-29 23:25                       ` Davide Libenzi
2002-10-29  1:47   ` Security critical race condition in epoll code John Gardiner Myers
2002-10-29  2:13     ` Davide Libenzi
2002-10-29  3:38     ` Davide Libenzi
2002-10-29 19:49   ` and nicer too - Re: [PATCH] epoll more scalable than poll John Gardiner Myers
2002-10-29 21:03     ` Davide Libenzi
2002-10-30  0:26       ` Jamie Lokier
2002-10-30  2:09         ` Davide Libenzi
2002-10-30  5:51         ` Davide Libenzi
2002-10-30  2:22       ` John Gardiner Myers
2002-10-30  3:51         ` Davide Libenzi
2002-10-31  2:07           ` John Gardiner Myers
2002-10-31  3:21             ` Davide Libenzi
2002-10-31 11:10               ` [Lse-tech] " Suparna Bhattacharya
2002-10-31 18:42                 ` Davide Libenzi
2002-10-30 23:01         ` Jamie Lokier
2002-10-30 23:53           ` Davide Libenzi
2002-10-31  0:52             ` Jamie Lokier
2002-10-31  4:15               ` Davide Libenzi
2002-10-31 15:07                 ` Jamie Lokier
2002-10-31 19:10                   ` Davide Libenzi
2002-11-01 17:42                     ` Dan Kegel
2002-11-01 17:45                       ` Davide Libenzi
2002-11-01 18:41                         ` Dan Kegel
2002-11-01 19:16                       ` Jamie Lokier
2002-11-01 20:04                         ` Charlie Krasic
2002-11-01 20:14                           ` Jamie Lokier
2002-11-01 20:22                         ` Mark Mielke
2002-10-31 15:41                 ` Unifying epoll,aio,futexes etc. (What I really want from epoll) Jamie Lokier
2002-10-31 15:48                   ` bert hubert
2002-10-31 16:45                   ` Alan Cox
2002-10-31 22:00                     ` Rusty Russell
2002-11-01  0:32                       ` Jamie Lokier
2002-11-01 13:23                       ` Alan Cox
2002-10-31 20:28                   ` Davide Libenzi
2002-10-31 23:02                     ` Jamie Lokier
2002-11-01  1:01                       ` Davide Libenzi
2002-11-01  2:01                         ` Jamie Lokier
2002-11-01 17:36                           ` Davide Libenzi
2002-11-01 20:45                         ` Jamie Lokier
2002-11-01  1:55                       ` Matthew D. Hall
2002-11-01  2:54                         ` Davide Libenzi
2002-11-01 18:18                           ` Dan Kegel
2002-11-01  2:56                         ` Jamie Lokier
2002-11-01  4:29                       ` Mark Mielke
2002-11-01  4:59                         ` Jamie Lokier
2002-11-01 23:27                       ` John Gardiner Myers
2002-11-02  4:55                         ` Mark Mielke
2002-11-02 15:41                         ` Jamie Lokier
2002-11-05 18:15                       ` pipe POLLOUT oddity John Gardiner Myers
2002-11-05 18:18                         ` Benjamin LaHaise
2002-11-01 23:16                   ` Unifying epoll,aio,futexes etc. (What I really want from epoll) John Gardiner Myers
2002-10-30 18:59       ` and nicer too - Re: [PATCH] epoll more scalable than poll Zach Brown
2002-10-30 19:25         ` Davide Libenzi
2002-10-31 16:54         ` Davide Libenzi

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=20021029004034.GA32118@outpost.ds9a.nl \
    --to=ahu@ds9a.nl \
    --cc=davidel@xmailserver.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lse-tech@lists.sourceforge.net \
    /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