Skip to contentSkip to conversation

Tidy up some client/server behaviors

PATCH In review Eric Sandeen 2 participants 12 messages Sep 17

lore
Share & tools
lore
#0 lore raw

cover Tidy up some client/server behaviors

Someone got happy with AI scanning and reported a bunch of items related to fio's client/server architecture - an architecture which is neither authenticated nor encrypted, and which *by design* allows arbitrary code execution, which is even documented in the man page.

A couple of the items stood out as probably worth fixing anyway, and I figured it might be worth being more explicit about the security posture of fio when the server is started listening on TCP, so that's patch 3.

I'm not wedded to any of this, tbh, so Jens if you don't like any of this I have no problem with just dropping any or all of these changes.

Thanks!

-Eric

#1 lore raw

PATCH 1/3 client: sanitize FIO_NET_CMD_SENDFILE requests

A legitimate fio server should only be requesting files from the client's cwd; reject absolute paths or any paths above the cwd.

Since this adds yet another failure mode, move fail: to the end of the function as is a bit more typical for a goto fail;

---
 client.c | 23 ++++++++++++++++++-----
 1 file changed, 18 insertions(+), 5 deletions(-)
client.c +18 -5
index 374a744a..b32c2852 100644
@@ -1809,12 +1809,18 @@ static int fio_send_file(struct fio_client *client, struct cmd_sendfile *pdu,
 	size = sizeof(*rep);
 	rep = malloc(size);
 
+	/* Sanitize requested path to reject files not under cwd */
+	if (pdu->path[0] == '/' ||
+	    strstr((const char *)pdu->path, "..") != NULL) {
+		log_err("fio: rejecting sendfile request for unsafe path: %s\n",
+			(const char *)pdu->path);
+		rep->error = EPERM;
+		goto fail;
+	}
+
 	if (stat((char *)pdu->path, &sb) < 0) {
-fail:
 		rep->error = errno;
-		sendfile_reply(client->fd, rep, size, tag);
-		free(rep);
-		return 1;
+		goto fail;
 	}
 
 	size += sb.st_size;
@@ -1822,14 +1828,21 @@ fail:
 	rep->size = cpu_to_le32((uint32_t) sb.st_size);
 
 	fd = open((char *)pdu->path, O_RDONLY);
-	if (fd == -1 )
+	if (fd == -1) {
+		rep->error = errno;
 		goto fail;
+	}
 
 	rep->error = read_data(fd, &rep->data, sb.st_size);
 	sendfile_reply(client->fd, rep, size, tag);
 	free(rep);
 	close(fd);
 	return 0;
+
+fail:
+	sendfile_reply(client->fd, rep, size, tag);
+	free(rep);
+	return 1;
 }
 
 int fio_handle_client(struct fio_client *client)
Signature · 1 line
2.55.0
#2 lore raw

Re: [PATCH 1/3] client: sanitize FIO_NET_CMD_SENDFILE requests

Quoted 17 lines · Eric Sandeen

A legitimate fio server should only be requesting files from the client's cwd; reject absolute paths or any paths above the cwd.

Since this adds yet another failure mode, move fail: to the end of the function as is a bit more typical for a goto fail;

---
 client.c | 23 ++++++++++++++++++-----
 1 file changed, 18 insertions(+), 5 deletions(-)
client.c +0 -0
index 374a744a..b32c2852 100644
@@ -1809,12 +1809,18 @@ static int fio_send_file(struct fio_client *client, struct cmd_sendfile *pdu,
 	size = sizeof(*rep);

client.c static int fio_send_file(struct fio_client *client, struct cmd_sendfile *pdu, · line 1819

Quoted 10 lines
client.c +8 -0
 	rep = malloc(size);
 
+	/* Sanitize requested path to reject files not under cwd */
+	if (pdu->path[0] == '/' ||
+	    strstr((const char *)pdu->path, "..") != NULL) {
+		log_err("fio: rejecting sendfile request for unsafe path: %s\n",
+			(const char *)pdu->path);
+		rep->error = EPERM;
+		goto fail;
+	}

Won't this potentially break existing use cases?

Signature · 1 line
Jens Axboe
#3 lore raw

Re: [PATCH 1/3] client: sanitize FIO_NET_CMD_SENDFILE requests

Quoted 20 lines · Jens Axboe
Quoted 19 lines · Eric Sandeen

A legitimate fio server should only be requesting files from the client's cwd; reject absolute paths or any paths above the cwd.

Since this adds yet another failure mode, move fail: to the end of the function as is a bit more typical for a goto fail;

---
 client.c | 23 ++++++++++++++++++-----
 1 file changed, 18 insertions(+), 5 deletions(-)
client.c +0 -0
index 374a744a..b32c2852 100644
@@ -1809,12 +1809,18 @@ static int fio_send_file(struct fio_client *client, struct cmd_sendfile *pdu,
 	size = sizeof(*rep);
 	rep = malloc(size);
 
Quoted 10 lines

client.c static int fio_send_file(struct fio_client *client, struct cmd_sendfile *pdu, · line 1819

Quoted 8 lines
client.c +8 -0
+	/* Sanitize requested path to reject files not under cwd */
+	if (pdu->path[0] == '/' ||
+	    strstr((const char *)pdu->path, "..") != NULL) {
+		log_err("fio: rejecting sendfile request for unsafe path: %s\n",
+			(const char *)pdu->path);
+		rep->error = EPERM;
+		goto fail;
+	}

Won't this potentially break existing use cases?

TBH I did this a while ago and never sent it, so let me re-check. I thought I had considered that at the time...

OK yeah, I guess it is too broad, I suppose if a job name has ".." in it, this would cause improper rejections. I don't think anything legitimately tries to grab files on relative paths, though.

I'm guessing you think this isn't worth it either, and if so again I'm happy to just drop it. Or I could tighten it up to only catch things that really try to grab arbitrary paths outside of cwd.

I'm also leaning towards not worth it. :)

-Eric

#4 lore raw

Re: [PATCH 1/3] client: sanitize FIO_NET_CMD_SENDFILE requests

Quoted 38 lines · Eric Sandeen
Quoted 20 lines · Jens Axboe
Quoted 19 lines · Eric Sandeen

A legitimate fio server should only be requesting files from the client's cwd; reject absolute paths or any paths above the cwd.

Since this adds yet another failure mode, move fail: to the end of the function as is a bit more typical for a goto fail;

---
 client.c | 23 ++++++++++++++++++-----
 1 file changed, 18 insertions(+), 5 deletions(-)
client.c +0 -0
index 374a744a..b32c2852 100644
@@ -1809,12 +1809,18 @@ static int fio_send_file(struct fio_client *client, struct cmd_sendfile *pdu,
 	size = sizeof(*rep);
 	rep = malloc(size);
 
Quoted 10 lines

client.c static int fio_send_file(struct fio_client *client, struct cmd_sendfile *pdu, · line 1819

Quoted 8 lines
client.c +8 -0
+	/* Sanitize requested path to reject files not under cwd */
+	if (pdu->path[0] == '/' ||
+	    strstr((const char *)pdu->path, "..") != NULL) {
+		log_err("fio: rejecting sendfile request for unsafe path: %s\n",
+			(const char *)pdu->path);
+		rep->error = EPERM;
+		goto fail;
+	}

Won't this potentially break existing use cases?

TBH I did this a while ago and never sent it, so let me re-check. I thought I had considered that at the time...

OK yeah, I guess it is too broad, I suppose if a job name has ".." in it, this would cause improper rejections. I don't think anything legitimately tries to grab files on relative paths, though.

Quoted 5 lines

I'm guessing you think this isn't worth it either, and if so again I'm happy to just drop it. Or I could tighten it up to only catch things that really try to grab arbitrary paths outside of cwd.

I'm also leaning towards not worth it. :)

Not worth it.

Signature · 1 line
Jens Axboe
#5 lore raw

PATCH 2/3 client: don't send uninitialized data in fio_send_file error replies

In reply to Eric Sandeen

Various error cases in fio_send_file() might send an uninitialized buffer over the wire. The server should ignore this when errno is set, but it's best practice to not ship heap data over the wire, so fix that by:

- using calloc vs malloc in the initial allocation so that early error paths send initialized data.

- grow the buffer via realloc only after open() succeeds, so that an open() failure doesn't send a grown/uninit buffer back with the error.

---
 client.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
client.c +4 -5
index b32c2852..a949b5a3 100644
@@ -1807,7 +1807,7 @@ static int fio_send_file(struct fio_client *client, struct cmd_sendfile *pdu,
 	int fd;
 
 	size = sizeof(*rep);
-	rep = malloc(size);
+	rep = calloc(1, size);
 
 	/* Sanitize requested path to reject files not under cwd */
 	if (pdu->path[0] == '/' ||
@@ -1823,16 +1823,15 @@ static int fio_send_file(struct fio_client *client, struct cmd_sendfile *pdu,
 		goto fail;
 	}
 
-	size += sb.st_size;
-	rep = realloc(rep, size);
-	rep->size = cpu_to_le32((uint32_t) sb.st_size);
-
 	fd = open((char *)pdu->path, O_RDONLY);
 	if (fd == -1) {
 		rep->error = errno;
 		goto fail;
 	}
 
+	size += sb.st_size;
+	rep = realloc(rep, size);
+	rep->size = cpu_to_le32((uint32_t) sb.st_size);
 	rep->error = read_data(fd, &rep->data, sb.st_size);
 	sendfile_reply(client->fd, rep, size, tag);
 	free(rep);
Signature · 1 line
2.55.0
#6 lore raw

PATCH 3/3 fio: server: Warn when listening on network

In reply to Eric Sandeen

Make it crystal clear to users at run time that the client/server protocol should only be used in trusted environments.

---
 server.c | 7 +++++++
 1 file changed, 7 insertions(+)
server.c +7 -0
index 34f3f5c0..887ed7ed 100644
@@ -2596,6 +2596,13 @@ static int fio_init_server_connection(void)
 	} else
 		snprintf(bind_str, sizeof(bind_str), "%s", bind_sock);
 
+	if (!bind_sock) {
+		log_info("fio: WARNING: The fio network protocol is neither "
+			 "authenticated nor encrypted.\n");
+		log_info("fio: WARNING: Use this mode only in trusted "
+			 "environments.\n");
+	}
+
 	log_info("fio: server listening on %s\n", bind_str);
 
 	if (listen(sk, 4) < 0) {
Signature · 1 line
2.55.0
#7 lore raw

Re: [PATCH 3/3] fio: server: Warn when listening on network

Quoted 16 lines · Eric Sandeen

Make it crystal clear to users at run time that the client/server protocol should only be used in trusted environments.

---
 server.c | 7 +++++++
 1 file changed, 7 insertions(+)
server.c +0 -0
index 34f3f5c0..887ed7ed 100644
@@ -2596,6 +2596,13 @@ static int fio_init_server_connection(void)
 	} else
 		snprintf(bind_str, sizeof(bind_str), "%s", bind_sock);
 

server.c static int fio_init_server_connection(void) · line 2608

Quoted 10 lines
server.c +7 -0
+	if (!bind_sock) {
+		log_info("fio: WARNING: The fio network protocol is neither "
+			 "authenticated nor encrypted.\n");
+		log_info("fio: WARNING: Use this mode only in trusted "
+			 "environments.\n");
+	}
+
 	log_info("fio: server listening on %s\n", bind_str);
 
 	if (listen(sk, 4) < 0) {

Let's not pollute the log with that - just document it in the... documentation!

Like I said in a private email, anyone that runs a fio backend outside of a trusted local/test environment needs to get their head examined. By definition, since it's generally used to open block devices and read/write to them it's run as root. And since the environment should be trusted, so should the clients. Anyone thinking otherwise needs to get their head examined.

Signature · 1 line
Jens Axboe
#8 lore raw

Re: [PATCH 3/3] fio: server: Warn when listening on network

Quoted 26 lines · Jens Axboe
Quoted 25 lines · Eric Sandeen

Make it crystal clear to users at run time that the client/server protocol should only be used in trusted environments.

---
 server.c | 7 +++++++
 1 file changed, 7 insertions(+)
server.c +7 -0
index 34f3f5c0..887ed7ed 100644
@@ -2596,6 +2596,13 @@ static int fio_init_server_connection(void)
 	} else
 		snprintf(bind_str, sizeof(bind_str), "%s", bind_sock);
 
+	if (!bind_sock) {
+		log_info("fio: WARNING: The fio network protocol is neither "
+			 "authenticated nor encrypted.\n");
+		log_info("fio: WARNING: Use this mode only in trusted "
+			 "environments.\n");
+	}
+
 	log_info("fio: server listening on %s\n", bind_str);
 
Quoted 4 lines

server.c static int fio_init_server_connection(void) · line 2608

Quoted 1 line
server.c +0 -0
 	if (listen(sk, 4) < 0) {

Let's not pollute the log with that - just document it in the... documentation!

Which is implicitly already there in the man page, so it's probably covered. Could make it more explicit I suppose, tho that's still assuming users will read it, heh.

Quoted 6 lines

Like I said in a private email, anyone that runs a fio backend outside of a trusted local/test environment needs to get their head examined. By definition, since it's generally used to open block devices and read/write to them it's run as root. And since the environment should be trusted, so should the clients. Anyone thinking otherwise needs to get their head examined.

I don't disagree. Just floating the idea of making it crystal clear at runtime, and like I said, if you don't like the change, I'm happy to drop it.

Thanks,
-Eric

#9 lore raw

Re: [PATCH 3/3] fio: server: Warn when listening on network

Quoted 27 lines · Eric Sandeen
Quoted 26 lines · Jens Axboe
Quoted 25 lines · Eric Sandeen

Make it crystal clear to users at run time that the client/server protocol should only be used in trusted environments.

---
 server.c | 7 +++++++
 1 file changed, 7 insertions(+)
server.c +7 -0
index 34f3f5c0..887ed7ed 100644
@@ -2596,6 +2596,13 @@ static int fio_init_server_connection(void)
 	} else
 		snprintf(bind_str, sizeof(bind_str), "%s", bind_sock);
 
+	if (!bind_sock) {
+		log_info("fio: WARNING: The fio network protocol is neither "
+			 "authenticated nor encrypted.\n");
+		log_info("fio: WARNING: Use this mode only in trusted "
+			 "environments.\n");
+	}
+
 	log_info("fio: server listening on %s\n", bind_str);
 
Quoted 8 lines
Quoted 4 lines

server.c static int fio_init_server_connection(void) · line 2608

Quoted 1 line
server.c +0 -0
 	if (listen(sk, 4) < 0) {

Let's not pollute the log with that - just document it in the... documentation!

Which is implicitly already there in the man page, so it's probably covered. Could make it more explicit I suppose, tho that's still assuming users will read it, heh.

The only people that will read this one are the ones that see it in a log, breaking their scripts, not the intended audience. If it's already documented in the man page, then just point any report like that at the man page!

Quoted 10 lines
Quoted 6 lines

Like I said in a private email, anyone that runs a fio backend outside of a trusted local/test environment needs to get their head examined. By definition, since it's generally used to open block devices and read/write to them it's run as root. And since the environment should be trusted, so should the clients. Anyone thinking otherwise needs to get their head examined.

I don't disagree. Just floating the idea of making it crystal clear at runtime, and like I said, if you don't like the change, I'm happy to drop it.

I will just pick up patch 2/3, I think that one is obviously fine.

Signature · 1 line
Jens Axboe
#10 lore raw

Re: [PATCH 3/3] fio: server: Warn when listening on network

Quoted 5 lines · Jens Axboe
Quoted 3 lines

I don't disagree. Just floating the idea of making it crystal clear at runtime, and like I said, if you don't like the change, I'm happy to drop it.

I will just pick up patch 2/3, I think that one is obviously fine.

I guess that depends on patch 1 - so please do resend patch 2, and I will get it applied.

Signature · 1 line
Jens Axboe
#11 lore raw

Re: [PATCH 3/3] fio: server: Warn when listening on network

In reply to Jens Axboe
Quoted 33 lines · Jens Axboe
Quoted 32 lines · Eric Sandeen
Quoted 30 lines · Jens Axboe
Quoted 16 lines · Eric Sandeen

Make it crystal clear to users at run time that the client/server protocol should only be used in trusted environments.

---
 server.c | 7 +++++++
 1 file changed, 7 insertions(+)
server.c +0 -0
index 34f3f5c0..887ed7ed 100644
@@ -2596,6 +2596,13 @@ static int fio_init_server_connection(void)
 	} else
 		snprintf(bind_str, sizeof(bind_str), "%s", bind_sock);
 

server.c static int fio_init_server_connection(void) · line 2608

Quoted 10 lines
server.c +7 -0
+	if (!bind_sock) {
+		log_info("fio: WARNING: The fio network protocol is neither "
+			 "authenticated nor encrypted.\n");
+		log_info("fio: WARNING: Use this mode only in trusted "
+			 "environments.\n");
+	}
+
 	log_info("fio: server listening on %s\n", bind_str);
 
 	if (listen(sk, 4) < 0) {

Let's not pollute the log with that - just document it in the... documentation!

Quoted 8 lines
Quoted 3 lines

Which is implicitly already there in the man page, so it's probably covered. Could make it more explicit I suppose, tho that's still assuming users will read it, heh.

The only people that will read this one are the ones that see it in a log, breaking their scripts, not the intended audience. If it's already documented in the man page, then just point any report like that at the man page!

Fair.

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

Like I said in a private email, anyone that runs a fio backend outside of a trusted local/test environment needs to get their head examined. By definition, since it's generally used to open block devices and read/write to them it's run as root. And since the environment should be trusted, so should the clients. Anyone thinking otherwise needs to get their head examined.

Quoted 5 lines
Quoted 3 lines

I don't disagree. Just floating the idea of making it crystal clear at runtime, and like I said, if you don't like the change, I'm happy to drop it.

I will just pick up patch 2/3, I think that one is obviously fine.

Sounds good, thanks Jens!

-Eric