I’ve written my own web server in C. How can I bind it to port 80 without being root so that the security is not being compromised (buffer overflows etc.)?
Should I simply forward any traffic from another “stable” server that runs on port 80?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Using a forward proxy is indeed the easiest and most recommended solution. It also has the advantage of filtering horribly invalid requests before they even reach your self-written server.
In case your application uses the user’s ip address for something remember to retrieve it from whatever header your webserver uses (
X-Client-IPetc.). However, only do so for requests that really come from your webserver, otherwise users can spoof their IP. You can do so by checking if the request came from your IP and only check the header in this case or simply make your application bind to localhost.Another solution would be granting the program the
CAP_NET_BIND_SERVICEcapability. This requires root to usesetcap cap_net_bind_service=ep /path/to/the/executable– since the flag is stored in a filesystem attribute, it will be lost when copying the file to another system or recompiling the application.Of course you could also make your program setuid root and then switch to an unprivileged user right after calling
bind(). However, depending on how your program works and what it does this might not be a good idea – for example, if it needs to close and reopen the listening socket for some reason it would require a full restart of the process.