Skip to content

Commit

Permalink
bootstrapper/cpiofs: handle out of bound getpage request correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
moodyhunter committed Dec 24, 2023
1 parent d8c7b83 commit 32f2537
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
26 changes: 16 additions & 10 deletions userspace/programs/init/bootstrapper/cpiofs_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@ static int cpiofs_mount(rpc_server_t *server, mos_rpc_fs_mount_request *req, mos
MOS_UNUSED(server);
MOS_UNUSED(data);

if (unlikely(req->options) && strlen(req->options) > 0)
puts("cpio: mount options are not supported");
if (req->options && strlen(req->options) > 0 && strcmp(req->options, "defaults") != 0)
printf("cpio: mount option '%s' is not supported\n", req->options);

if (req->device && strcmp(req->device, "none") != 0)
puts("cpio: mount: dev_name is not supported");
if (req->device && strlen(req->device) > 0 && strcmp(req->device, "none") != 0)
printf("cpio: mount: device name '%s' is not supported\n", req->device);

cpio_inode_t *cpio_i = cpio_trycreate_i(".");
if (!cpio_i)
Expand Down Expand Up @@ -170,8 +170,6 @@ static int cpiofs_readdir(rpc_server_t *server, mos_rpc_fs_readdir_request *req,

if (found && !is_TRAILER && !is_root_dot)
{
printf("prefix '%s' filename '%s'\n", path_prefix, fpath);

const s64 ino = strntoll(header.ino, NULL, 16, sizeof(header.ino) / sizeof(char));
const u32 modebits = strntoll(header.mode, NULL, 16, sizeof(header.mode) / sizeof(char));
const pb_file_type_t type = cpio_modebits_to_filetype(modebits & CPIO_MODE_FILE_TYPE);
Expand Down Expand Up @@ -204,8 +202,6 @@ static int cpiofs_readdir(rpc_server_t *server, mos_rpc_fs_readdir_request *req,
}

resp->entries_count = n_written; // not the same as resp->entries_count, because we might have allocated more than we needed

printf("iterated with prefix='%s', found %zu entries\n", path_prefix, n_written);
return RPC_RESULT_OK;
}

Expand Down Expand Up @@ -272,6 +268,14 @@ static int cpiofs_getpage(rpc_server_t *server, mos_rpc_fs_getpage_request *req,
MOS_UNUSED(server);
MOS_UNUSED(data);

if (req->pgoff * MOS_PAGE_SIZE >= req->inode.stat.size)
{
resp->data = malloc(sizeof(pb_bytes_array_t));
resp->data->size = 0;
resp->result.success = true;
return RPC_RESULT_OK;
}

cpio_inode_t *cpio_i = (cpio_inode_t *) req->inode.private_data;
const size_t bytes_to_read = MIN((size_t) MOS_PAGE_SIZE, cpio_i->pb_i.stat.size - req->pgoff * MOS_PAGE_SIZE);

Expand Down Expand Up @@ -323,9 +327,11 @@ void cpiofs_run_server()
}

if (resp.result.success)
printf("cpiofs registered with filesystem server\n");
printf("cpiofs: registered with filesystem server\n");
else
printf("cpiofs failed to register with filesystem server\n");
printf("cpiofs: failed to register with filesystem server\n");

pb_release(mos_rpc_fs_register_response_fields, &resp);

rpc_server_exec(cpiofs);
puts("cpiofs server exited unexpectedly");
Expand Down
6 changes: 3 additions & 3 deletions userspace/programs/init/bootstrapper/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ int main(int argc, const char *argv[])
pid_t filesystem_server_pid = syscall_fork();
if (filesystem_server_pid == -1)
{
puts("failed to fork filesystem server");
puts("bootstrapper: failed to fork filesystem server");
return 0;
}
else if (filesystem_server_pid == 0)
{
puts("starting filesystem server");
puts("bootstrapper: starting filesystem server");
cpiofs_run_server();
puts("filesystem server exited unexpectedly");
puts("bootstrapper: filesystem server exited unexpectedly");
return 0;
}

Expand Down

0 comments on commit 32f2537

Please sign in to comment.