-
Notifications
You must be signed in to change notification settings - Fork 155
Expand file tree
/
Copy pathhvt_module_blk.c
More file actions
168 lines (145 loc) · 4.48 KB
/
hvt_module_blk.c
File metadata and controls
168 lines (145 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* Copyright (c) 2015-2019 Contributors as noted in the AUTHORS file
*
* This file is part of Solo5, a sandboxed execution environment.
*
* Permission to use, copy, modify, and/or distribute this software
* for any purpose with or without fee is hereby granted, provided
* that the above copyright notice and this permission notice appear
* in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
* WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
* AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* hvt_module_blk.c: Block device module.
*/
#define _GNU_SOURCE
#define _FILE_OFFSET_BITS 64
#include <assert.h>
#include <err.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#if HVT_FREEBSD_ENABLE_CAPSICUM
#include <sys/capsicum.h>
#endif
#include "../common/block_attach.h"
#include "hvt.h"
#include "solo5.h"
static bool module_in_use;
static struct mft *host_mft;
static void hypercall_block_write(struct hvt *hvt, hvt_gpa_t gpa)
{
struct hvt_hc_block_write *wr =
HVT_CHECKED_GPA_P(hvt, gpa, sizeof (struct hvt_hc_block_write));
struct mft_entry *e = mft_get_by_index(host_mft, wr->handle,
MFT_DEV_BLOCK_BASIC);
if (e == NULL) {
wr->ret = SOLO5_R_EINVAL;
return;
}
ssize_t ret;
off_t pos, end;
assert(wr->len <= SSIZE_MAX);
if (wr->offset >= e->u.block_basic.capacity) {
wr->ret = SOLO5_R_EINVAL;
return;
}
pos = wr->offset;
if (add_overflow(pos, wr->len, end)
|| (end > e->u.block_basic.capacity)) {
wr->ret = SOLO5_R_EINVAL;
return;
}
ret = pwrite(e->b.hostfd, HVT_CHECKED_GPA_P(hvt, wr->data, wr->len),
wr->len, pos);
assert(ret == wr->len);
wr->ret = SOLO5_R_OK;
}
static void hypercall_block_read(struct hvt *hvt, hvt_gpa_t gpa)
{
struct hvt_hc_block_read *rd =
HVT_CHECKED_GPA_P(hvt, gpa, sizeof (struct hvt_hc_block_read));
struct mft_entry *e = mft_get_by_index(host_mft, rd->handle,
MFT_DEV_BLOCK_BASIC);
if (e == NULL) {
rd->ret = SOLO5_R_EINVAL;
return;
}
ssize_t ret;
off_t pos, end;
assert(rd->len <= SSIZE_MAX);
if (rd->offset >= e->u.block_basic.capacity) {
rd->ret = SOLO5_R_EINVAL;
return;
}
pos = rd->offset;
if (add_overflow(pos, rd->len, end)
|| (end > e->u.block_basic.capacity)) {
rd->ret = SOLO5_R_EINVAL;
return;
}
ret = pread(e->b.hostfd, HVT_CHECKED_GPA_P(hvt, rd->data, rd->len), rd->len,
pos);
assert(ret == rd->len);
rd->ret = SOLO5_R_OK;
}
static int handle_cmdarg(char *cmdarg, struct mft *mft)
{
if (strncmp("--block:", cmdarg, 8) != 0)
return -1;
char name[MFT_NAME_SIZE];
char path[PATH_MAX + 1];
int rc = sscanf(cmdarg,
"--block:%" XSTR(MFT_NAME_MAX) "[A-Za-z0-9]="
"%" XSTR(PATH_MAX) "s", name, path);
if (rc != 2)
return -1;
struct mft_entry *e = mft_get_by_name(mft, name, MFT_DEV_BLOCK_BASIC, NULL);
if (e == NULL) {
warnx("Resource not declared in manifest: '%s'", name);
return -1;
}
off_t capacity;
int fd = block_attach(path, &capacity);
e->u.block_basic.capacity = capacity;
e->u.block_basic.block_size = 512;
e->b.hostfd = fd;
e->attached = true;
module_in_use = true;
return 0;
}
static int setup(struct hvt *hvt, struct mft *mft)
{
if (!module_in_use)
return 0;
host_mft = mft;
assert(hvt_core_register_hypercall(HVT_HYPERCALL_BLOCK_WRITE,
hypercall_block_write) == 0);
assert(hvt_core_register_hypercall(HVT_HYPERCALL_BLOCK_READ,
hypercall_block_read) == 0);
#if HVT_FREEBSD_ENABLE_CAPSICUM
cap_rights_t rights;
cap_rights_init(&rights, CAP_READ, CAP_WRITE, CAP_SEEK);
if (cap_rights_limit(diskfd, &rights) == -1)
err(1, "cap_rights_limit() failed");
#endif
return 0;
}
static char *usage(void)
{
return "--block:NAME=PATH (attach block device/file at PATH as block storage NAME)";
}
DECLARE_MODULE(block,
.setup = setup,
.handle_cmdarg = handle_cmdarg,
.usage = usage
)