From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on gnuweeb.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=ALL_TRUSTED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NO_DNS_FOR_FROM,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.6 Received: from integral2.. (unknown [36.81.65.188]) by gnuweeb.org (Postfix) with ESMTPSA id 9D05E7E32F; Fri, 8 Jul 2022 12:11:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1657282272; bh=3KkeDCEn1AlLDCRM7aYi/vmiAM5pVF7mcv7pi0L+NRU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=XbCccelTFkxn7uM/+zkpJRlbrcwB6uVcKKBdQV1q2lfH91/iZVlK1cFGR4p7Y7YLK HAbUqb8BGlwV1iEd8D0uD70f5pT8oSuAnKXVLxQ3/zISHjUUJ1lBy0GNqeXd+uTWGE tabBQldl49ZBC/HMmn2e1ZpERkQ1Qvbh4QpCGpol/mR5Bh4pa8ZXASxBgDujMMh00M 5Dg5RQ19B+682Y68zIJOvfzVNeG44yHw3LNYftE1vkhmjDr42iKshDOrJj2JGK/1iu Evi+W7Md/t9GcDzKbiJoYpoYCt+5yIQ3LhS28eSVTvPFudb32fAyxzN5fMtv6FSvz7 diyOwTT8U0wjQ== From: Ammar Faizi To: GNU/Weeb Mailing List Cc: Ammar Faizi , Alviro Iskandar Setiawan , Arthur Lapz , Fernanda Ma'rouf , Sprite , Yonle , Hendra Manudinata Subject: [PATCH gwhttpd 13/14] gwhttpd: Fix 403 HTTP error when accessing an empty file Date: Fri, 8 Jul 2022 19:10:24 +0700 Message-Id: <20220708121025.926162-14-ammarfaizi2@gnuweeb.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220708121025.926162-1-ammarfaizi2@gnuweeb.org> References: <20220708121025.926162-1-ammarfaizi2@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: 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 Reported-by: Hendra Manudinata Signed-off-by: Ammar Faizi --- 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