commit 5fb79fd9d7f32a419915d26072224892f37138bc
parent 2ae1a0b5a78b7bd8a2bc63174658cdb1db44a153
Author: Remy Noulin <loader2x@gmail.com>
Date: Thu, 19 Jan 2023 21:37:40 +0200
Issue error not found when the begining of the request path is correct with extra / and non existing path
realpath() expands all symbolic links and resolves references to /./,
/../ and extra '/' characters in the null-terminated string named by
path to produce a canonicalized absolute pathname.
So the realpath result becomes a correct path.
For example, there is README.md in server root:
A request with the following path /README.md/asdasd now fails.
Before this commit, the server would serve /README.md because of the
realpath result.
spartserv.c | 7 +++++++
spartservPrivDrop.c | 11 ++++++++++-
2 files changed, 17 insertions(+), 1 deletion(-)
Diffstat:
2 files changed, 17 insertions(+), 1 deletion(-)
diff --git a/spartserv.c b/spartserv.c
@@ -21,6 +21,7 @@ text/markdown .md .markdown
#include <stdarg.h>
#include <limits.h>
#include <time.h>
+#include <fcntl.h> // access
// inet_ntoa
//already included #include <sys/socket.h>
@@ -263,6 +264,12 @@ int main(int ac, char **av){
memcpy(cursor, path, strlen(path));
// check path
+ if (access(localPath, R_OK) == -1) {
+ puts("4 Not found");
+ send(mysock, "4 Not found\r\n", sizeof("4 Not found\r\n"), MSG_NOSIGNAL);
+ close(mysock);
+ continue;
+ }
char realPath[PATH_MAX] = {0};
realpath(localPath, realPath);
if (memcmp(realPath, root, rootLen-slash) != 0) {
diff --git a/spartservPrivDrop.c b/spartservPrivDrop.c
@@ -14,13 +14,13 @@ text/markdown .md .markdown
#include <netinet/in.h>
#include <stdlib.h>
#include <unistd.h>
-#include <sys/types.h>
#include <sys/stat.h>
#include <stdbool.h>
#include <ctype.h>
#include <stdarg.h>
#include <limits.h>
#include <time.h>
+#include <fcntl.h> // access
// inet_ntoa
//already included #include <sys/socket.h>
@@ -146,6 +146,9 @@ static struct sock_filter filter_kore[] = {
KORE_SYSCALL_ALLOW(write),
KORE_SYSCALL_ALLOW(close),
KORE_SYSCALL_ALLOW(openat),
+#if defined(SYS_access)
+ KORE_SYSCALL_ALLOW(access),
+#endif
#if defined(SYS_send)
KORE_SYSCALL_ALLOW(send),
#endif
@@ -490,6 +493,12 @@ int main(int ac, char **av){
memcpy(cursor, path, strlen(path));
// check path
+ if (access(localPath, R_OK) == -1) {
+ puts("4 Not found");
+ send(mysock, "4 Not found\r\n", sizeof("4 Not found\r\n"), MSG_NOSIGNAL);
+ close(mysock);
+ continue;
+ }
char realPath[PATH_MAX] = {0};
realpath(localPath, realPath);
if (memcmp(realPath, root, rootLen-slash) != 0) {