News for hackers. Stuff that matters.
Re:TCP HTTP Server written in Assembly by tokenizer on Saturday December 14, 2013 @09:42PM (#6908112)
As a web developer who isn't familiar with assembly or any web server more barebones than nginx, what benefits does something like this provide? Speed? Could this be a solution for an extremely simple directory/static file web server?
Re:TCP HTTP Server written in Assembly by anonymouscowar1 on Saturday December 14, 2013 @09:50PM (#6908149)
This is a simple, single-threaded single-process accept-read-respond-loop web server. It's vulnerable to trivial trickle DoS attacks and probably has other issues. There are no advantages, the author just did this for fun.

The TCP part comes from C code in the kernel, so this headline is a little misleading ;-).

Re:TCP HTTP Server written in Assembly by kragen on Sunday December 15, 2013 @11:41AM (#6909907)
Agreed. However, it should be safe from buffer overflows, path traversal attacks, XSS, and obviously CSRF. It should be fine other than DoS. Let me know if you find any exceptions.
Re:TCP HTTP Server written in Assembly by anonymouscowar1 on Sunday December 15, 2013 @04:21PM (#6910940)
It's hard to be vulnerable to XSS and CSRF with all-static content, no?

So, not only will a trickle DoS other clients, each byte will also force an O(n) traversal of $buf (burning CPU). Granted, buf is only 1000 bytes, but that's not great.

It looks like a request with no space could force you to walk (`repne scasb`) through invalid memory after $buf. Also maybe corrupt it (unescape_request_path).

It will also fail to correctly parse HTTP/0.9 (not a big deal, but part of spec). The parsing code ignores the existence of verbs other than GET. (Doesn't check that the verb is GET either.)

We don't validate that paths start with /, we just skip that byte. Okay:

        mov (path), %al
        ...
        cmp $'/, %al
        je badreq
Since valid GETs are of the form:

    GET /foo.txt HTTP/1.0
         ^-- path=buf+5
As you point out, a client close will cause SIGPIPE causing a crash (DoS).

That's all I see. But I'm not an asm expert and I'm sure I've missed something.

Re:TCP HTTP Server written in Assembly by kragen on Sunday December 15, 2013 @10:56PM (#6912757)
> It's hard to be vulnerable to XSS and CSRF with all-static content, no?

You would think, but actually Apache managed to be vulnerable to XSS by including bits of the request URL in its error paegs, if I remember right. Last millennium, I think.

> So, not only will a trickle DoS other clients, each byte will also force an O(n) traversal of $buf (burning CPU). Granted, buf is only 1000 bytes, but that's not great.

Hmm, while I hadn't thought about that, and I should have, I think that's probably okay; basically you're saying that you can get the machine to burn up to, say, 2048 cycles by sending it a small TCP packet. Which means that a 4-core 2GHz server machine can't handle more than about four million packets per second (well, one million until I parallelize), which is about 85 megabytes per second, or 680 megabits per second. There are probably other bottlenecks in the code, the kernel, or your data center that will kick in first. It's probably more effective to DoS the server by just requesting files from it.

> It looks like a request with no space could force you to walk (`repne scasb`) through invalid memory after $buf.

It's possible I could have gotten this wrong, but I did try to limit the number of bytes it would scan to the bytes that it had actually read, by doing

    mov (bufp), %ecx
before the repne scasb. Did I screw that up?

> HTTP/0.9 ...verbs other than GET.

Yes, those are unimplemented features, and you're right that their lack makes the server behave incorrectly; hopefully they don't result in security bugs. I think they don't matter in practice, since nobody sends HTTP/0.9 requests or HEAD requests, except by hand, do they?

> We don't validate that paths start with /, we just skip that byte.

Right. And the $'/ check below is to keep you from saying

    GET //etc/passwd HTTP/1.0
and getting /etc/passwd. In case that matters in 2013.

Thank you very much for looking over it!

Re:TCP HTTP Server written in Assembly by bebna on Monday December 16, 2013 @06:49AM (#6914182)
Didn't send ab HEAD requests?

I know it does this by a given flag, but in some tests I have seen some HEADs between my GETs. I haven't used ab for long time, so don't quote me on that. Have u tried httpress[1] as a benchmark tool?

How about a simple check against the first byte equals G (DEC 71) if it is a GET? Shouldn't be that expensive, I think.

Thanks for creating it.

[1] https://bitbucket.org/yarosla/httpress/wiki/Home

Re:TCP HTTP Server written in Assembly by kragen on Monday December 16, 2013 @11:09PM (#6919583)
I don't know if ab sends HEAD requests! Thanks for the link to httpress; I've been having trouble with ab failing at high concurrencies (1000 concurrent connections) and also being the bottleneck.
Re:TCP HTTP Server written in Assembly by anonymouscowar1 on Monday December 16, 2013 @01:08PM (#6916551)
> before the repne scasb. Did I screw that up?

Ah, it's possible repne scasb halts when ecx drops to zero (that would explain some of the string length asm code I found when I googled it). I'm not very familiar with x86 mneumonics apart from the basics ('mov').

Re:TCP HTTP Server written in Assembly by anonymouscowar1 on Monday December 16, 2013 @01:07PM (#6916536)
> four million packets per second (well, one million until I parallelize), which is about 85 megabytes per second

Why is this more than 4 million bytes per second (4 MB/s)? A packet can contain a single byte.

Re:TCP HTTP Server written in Assembly by kragen on Monday December 16, 2013 @03:16PM (#6917509)
To be a valid TCP packet, it needs to contain at a minimum a 20-byte IP header and a 20-byte TCP header, plus the one byte of payload. In practice your server is probably receiving the packet over Ethernet, so it probably has an Ethernet header and things like that, too, but that's a minimum. You could approach it over, say, SLIP.
Re:TCP HTTP Server written in Assembly by knappador on Sunday December 15, 2013 @12:28AM (#6908494)
This is normally the kind of question I ask about anything involving HTML/CSS only or JS only =D PoC's based on low-level concepts are the ones that make you curious about everything from top to bottom. Even though assembly is the least abstract and most esoteric of programming (some would argue opposite) spaces, the program actually reveals itself quite quickly knowing just a few tid-bits. This is how you get to see that even the most low-level aspects of programming are quite accessible.
Re:TCP HTTP Server written in Assembly by kragen on Sunday December 15, 2013 @11:42AM (#6909910)
Thank you! I'm glad to hear that you found it so readable.
Re:TCP HTTP Server written in Assembly by kragen on Sunday December 15, 2013 @11:39AM (#6909897)
Nginx will almost certainly be faster, and is somewhat robust against DoS attacks. I didn't write this to provide benefits. There are situations where this would work better than nginx (where, say, you don't want to spend any time configuring anything) but there are better existing solutions for those cases.
Re:TCP HTTP Server written in Assembly by hcarvalhoalves on Saturday December 14, 2013 @09:45PM (#6908128)
This is obviously a toy, or PoC.
Re:TCP HTTP Server written in Assembly by ars_technician on Saturday December 14, 2013 @09:47PM (#6908137)
I don't know about calling it a piece of crap. Seems a little harsh. :-)
Re:TCP HTTP Server written in Assembly by optymizer on Saturday December 14, 2013 @11:16PM (#6908357)
I don't normally see 'piece of crap' written as PoC. I'm used to seeing PoS used often. I usually see PoC as 'proof of concept'.
Re:TCP HTTP Server written in Assembly by jamestanderson on Saturday December 14, 2013 @09:50PM (#6908147)
It could mean proof of concept.
Re:TCP HTTP Server written in Assembly by wanda on Saturday December 14, 2013 @10:00PM (#6908173)
I think that smile could mean that he's being facetious.
Re:TCP HTTP Server written in Assembly by solistice on Saturday December 14, 2013 @11:30PM (#6908388)
Incidentally, I read it the other way as well.
Re:TCP HTTP Server written in Assembly by tinco on Sunday December 15, 2013 @07:39AM (#6909188)
Haha, imagine what tech news would be like if every time you saw PoC you thought it meant piece of crap. Quite hilarious actually.
Re:TCP HTTP Server written in Assembly by UNIXgod on Saturday December 14, 2013 @11:31PM (#6908392)
Definitely meant it's ROFL web scale, asynchronous, non-blocking, event driven, message passing, nosql, sharded, clusters of highly available, reliable, high performance, real time, bad ass, rockstar, get the girls, get the funding, get the IPO, impress your mom, impress your cat ... applications.