public inbox for [email protected]
 help / color / mirror / Atom feed
From: Ammar Faizi <[email protected]>
To: GNU/Weeb Mailing List <[email protected]>
Cc: Ammar Faizi <[email protected]>,
	Alviro Iskandar Setiawan <[email protected]>,
	Arthur Lapz <[email protected]>,
	Fernanda Ma'rouf <[email protected]>,
	Sprite <[email protected]>, Yonle <[email protected]>,
	Hendra Manudinata <[email protected]>
Subject: [PATCH gwhttpd 13/14] gwhttpd: Fix 403 HTTP error when accessing an empty file
Date: Fri,  8 Jul 2022 19:10:24 +0700	[thread overview]
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>

Hendra reports that he is seeing an HTTP 403 error when accssing an
empty file. It turned out that we fail on the mmap() syscall. mmap()
returns -EINVAL if the given size is zero. This happens when the file
size is zero. Make a special case when the file size is zero, bump the
mmap() size to 1. This way the mmap() will happily allocate a VMA
without error.

Link: https://t.me/GNUWeeb/634242
Tested-by: Hendra Manudinata <[email protected]>
Reported-by: Hendra Manudinata <[email protected]>
Signed-off-by: Ammar Faizi <[email protected]>
---
 gwhttpd.cpp | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/gwhttpd.cpp b/gwhttpd.cpp
index 4a6db62..7cc5dfe 100644
--- a/gwhttpd.cpp
+++ b/gwhttpd.cpp
@@ -888,6 +888,7 @@ static char *open_file_for_stream(const char *file, struct stat *st,
 				  struct client_sess *sess,
 				  struct stream_file_data *sfd)
 {
+	size_t mmap_size;
 	char *map;
 	int ret;
 	int fd;
@@ -911,7 +912,12 @@ static char *open_file_for_stream(const char *file, struct stat *st,
 		return NULL;
 	}
 
-	map = (char *)mmap(NULL, st->st_size, PROT_READ, MAP_SHARED, fd, 0);
+	if (st->st_size == 0)
+		mmap_size = 1;
+	else
+		mmap_size = st->st_size;
+
+	map = (char *)mmap(NULL, mmap_size, PROT_READ, MAP_SHARED, fd, 0);
 	close(fd);
 	if (unlikely(map == MAP_FAILED)) {
 		ret = errno;
@@ -1071,15 +1077,23 @@ static int start_stream_file(const char *file, struct client_sess *sess,
 
 	handle_range_header(sess, &start_offset);
 	if (start_offset >= sfd.size || start_offset < 0) {
+		if (sfd.size == 0)
+			goto send_hdr;
 		stream_file_bad_req(sess, "Bad range offset!");
 		return -EBADMSG;
 	}
 
+send_hdr:
 	sfd.cur_off = start_offset;
 	ret = send_http_header_for_stream_file(sess, start_offset, st.st_size);
 	if (unlikely(ret))
 		return -EBADMSG;
 
+	if (sfd.size == 0) {
+		munmap(sfd.map, 1);
+		return 1;
+	}
+
 	if (st.st_size <= max_send_len_file) {
 		ret = stream_file_once(&sfd, sess);
 		munmap(sfd.map, sfd.size);
-- 
Ammar Faizi


  parent reply	other threads:[~2022-07-08 12:11 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-08 12:10 [PATCH gwhttpd 00/14] gwhttpd updates Ammar Faizi
2022-07-08 12:10 ` [PATCH gwhttpd 01/14] gwhttpd: Do an early return when `parse_http_header()` fails Ammar Faizi
2022-07-08 12:10 ` [PATCH gwhttpd 02/14] gwhttpd: Don't print any error when mlock fails Ammar Faizi
2022-07-08 12:10 ` [PATCH gwhttpd 03/14] gwhttpd: Replace `send_error_and_close()` with `send_http_error()` Ammar Faizi
2022-07-08 12:10 ` [PATCH gwhttpd 04/14] gwhttpd: Add log in the interrupt handler Ammar Faizi
2022-07-08 12:10 ` [PATCH gwhttpd 05/14] gwhttpd: Refactor HTTP header parser Ammar Faizi
2022-07-08 12:10 ` [PATCH gwhttpd 06/14] gwhttpd: Avoid endless busy spinning on `send()` Ammar Faizi
2022-07-08 12:10 ` [PATCH gwhttpd 07/14] Makefile: Add "make clean" command Ammar Faizi
2022-07-08 12:10 ` [PATCH gwhttpd 08/14] gwhttpd: Skip interrupt error from `epoll_wait()` Ammar Faizi
2022-07-08 12:10 ` [PATCH gwhttpd 09/14] gwhttpd: Add directory listing and download file support Ammar Faizi
2022-07-08 12:10 ` [PATCH gwhttpd 10/14] gwhttpd: Add command line options Ammar Faizi
2022-07-08 12:10 ` [PATCH gwhttpd 11/14] gwhttpd: Add SLC support Ammar Faizi
2022-07-08 12:10 ` [PATCH gwhttpd 12/14] gwhttpd: slc: Shut the SLC log up Ammar Faizi
2022-07-08 12:10 ` Ammar Faizi [this message]
2022-07-08 12:10 ` [PATCH gwhttpd 14/14] gwhttpd: Add connecting log for SLC Ammar Faizi

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 \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    /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