Skip to content

About

syscall-httpd is a small concurrent HTTP/1.0 server written in x86-64 Linux assembly (GNU `as`, Intel syntax). No libc and no dependencies: it talks to the kernel only through raw syscalls. The build produces a static binary.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

syscall-httpd

asmhttpd is a small concurrent HTTP/1.0 server written in x86-64 Linux assembly (GNU as, Intel syntax). No libc and no dependencies: it talks to the kernel only through raw syscalls, and links into a static binary of a few kilobytes.

Inspired by the web-server exercises in pwn.college's Computing 101 course.

Features

  • GET: serves regular files of any size, streamed in 4 KiB chunks, with a correct Content-Length. / maps to index.html. Query strings are ignored.
  • POST: writes exactly Content-Length bytes of the request body into the target file (created or truncated, mode 0644), even when the body arrives over several packets.
  • Concurrency: one process per connection (fork). SIGCHLD is ignored, so the kernel reaps children and no zombies remain.
  • Errors: 400 Bad Request, 403 Forbidden, 404 Not Found, 405 Method Not Allowed (with an Allow header), 500 Internal Server Error.
  • Safety checks: any path containing .. is rejected with 403; directories and other non-regular files are rejected with 403.
  • Robustness: SIGPIPE is ignored, so a client that disconnects early can't kill the server. SO_REUSEADDR lets you restart it on the same port at once. Partial writes are retried until every byte is sent.

Build

Requires an x86-64 Linux machine (or WSL) with GNU binutils (as, ld) and make.

make          # assembles every src/*.s into build/ and links ./asmhttpd
make test     # runs the end-to-end tests (needs bash, curl, nc, cmp)
make clean

Usage

./asmhttpd [port] [root_dir]
Argument Default Meaning
port 8080 TCP port, bound on 0.0.0.0
root_dir . directory to serve; the server chdirs into it
./asmhttpd 8080 ./www &
curl http://127.0.0.1:8080/                          # www/index.html
curl --data 'hello' http://127.0.0.1:8080/note.txt   # writes www/note.txt
curl http://127.0.0.1:8080/note.txt                  # hello

Architecture

Source layout

include/defs.inc   syscall numbers, socket/file/signal constants, buffer sizes
src/main.s         _start, argument parsing, signal setup, accept loop, fork
src/net.s          setup_listener: socket, SO_REUSEADDR, bind, listen
src/http.s         handle_client, parse_path, find_header_end, parse_content_length
src/handlers.s     handle_get, handle_post
src/util.s         write_all, atoi, itoa, print_listening
src/data.s         response strings (.rodata), sockaddr/sigaction (.data), buffers (.bss)

Every module includes defs.inc. Symbols used across files are declared .global; internal functions follow the System V AMD64 calling convention.

Process model

                         ┌────────────────────────────────┐
                         │ parent: _start                 │
                         │  chdir(root_dir)               │
                         │  ignore SIGCHLD, SIGPIPE       │
                         │  socket / bind / listen        │
                         └───────────────┬────────────────┘
                                         │
                   ┌────────────────────►▼
                   │              accept(listen_fd)
                   │                     │ client_fd
                   │                     ▼
                   │                  fork()
                   │          ┌──────────┴───────────┐
                   │     pid > 0                  pid == 0
                   │          │                      │
                   │          ▼                      ▼
                   │   close(client_fd)     ┌───────────────────────┐
                   └──────────┘             │ child                 │
                                            │  close(listen_fd)     │
                                            │  handle_client        │
                                            │   ├─ read headers     │
                                            │   ├─ GET  → handle_get│
                                            │   └─ POST → handle_post
                                            │  close(client_fd)     │
                                            │  exit(0)              │
                                            └───────────────────────┘
                                 children reaped by the kernel (SIGCHLD = SIG_IGN)

The parent only accepts and forks; each child owns exactly one connection and exits when it is done, so a slow or failing client never blocks the others.

Limitations

  • HTTP/1.0 only: one request per connection, always Connection: close; no keep-alive, no chunked transfer encoding.
  • Only GET and POST. No HEAD, PUT, DELETE, ranges or conditional requests.
  • Request line and headers must fit in 4 KiB, or the server answers 400.
  • No Content-Type header is sent; clients have to guess the media type.
  • No URL decoding: %20 and similar are taken literally.
  • POST overwrites any writable file under the root, with no authentication. Don't expose it to an untrusted network.
  • The .. check is a plain substring test, so a name like a..b is also refused. Symlinks inside the root are followed.
  • No timeouts and no limit on the number of children: a client that never sends its headers keeps a process alive, and a flood of connections can exhaust the process table.
  • Linux x86-64 only (raw syscall numbers and struct stat offsets).

License

MIT

About

syscall-httpd is a small concurrent HTTP/1.0 server written in x86-64 Linux assembly (GNU `as`, Intel syntax). No libc and no dependencies: it talks to the kernel only through raw syscalls. The build produces a static binary.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages