Skip to content

Commit a9a862c

Browse files
committed
nitro: Write rootfs tar archive to enclave
1 parent 38d2a22 commit a9a862c

3 files changed

Lines changed: 79 additions & 9 deletions

File tree

init/nitro/main.c

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <stdint.h>
88
#include <stdio.h>
99
#include <stdlib.h>
10+
#include <string.h>
1011
#include <unistd.h>
1112
#include <sys/mount.h>
1213
#include <sys/reboot.h>
@@ -177,23 +178,59 @@ krun_signal()
177178
// communication is established.
178179
ret = write(sock_fd, buf, 1);
179180
if (ret != 1) {
181+
close(sock_fd);
180182
perror("vsock write");
181183
return -errno;
182184
}
183185

184186
ret = read(sock_fd, buf, 1);
185187
if (ret != 1) {
188+
close(sock_fd);
186189
perror("vsock read");
187190
return -errno;
188191
}
189192

190-
if (buf[0] != HEART_BEAT)
193+
if (buf[0] != HEART_BEAT) {
194+
close(sock_fd);
191195
return -1;
196+
}
192197

193-
ret = close(sock_fd);
194-
if (ret < 0) {
195-
perror("vsock close");
196-
return -errno;
198+
return sock_fd;
199+
}
200+
201+
int
202+
rootfs_rcv(int sock_fd)
203+
{
204+
uint8_t len_b[8], *rootfs_archive;
205+
ssize_t read_len;
206+
uint64_t len, idx = 0;
207+
int ret;
208+
209+
read_len = read(sock_fd, len_b, 8);
210+
if (read_len < 8) {
211+
close(sock_fd);
212+
perror("vsock rootfs archive length read");
213+
return -1;
214+
}
215+
216+
memcpy(&len, len_b, sizeof(uint64_t));
217+
218+
rootfs_archive = (uint8_t *) malloc(len);
219+
if (rootfs_archive == NULL) {
220+
close(sock_fd);
221+
perror("rootfs archive malloc");
222+
return -1;
223+
}
224+
225+
while (len) {
226+
read_len = read(sock_fd, &rootfs_archive[idx], len);
227+
if (read_len <= 0) {
228+
close(sock_fd);
229+
perror("vsock rootfs archive read");
230+
return -1;
231+
}
232+
idx += read_len;
233+
len -= read_len;
197234
}
198235

199236
return 0;
@@ -202,7 +239,7 @@ krun_signal()
202239
int
203240
main(int argc, char *argv[])
204241
{
205-
int ret;
242+
int ret, sock_fd;
206243

207244
// Block all signals.
208245
ret = sig_mask(SIG_BLOCK);
@@ -219,7 +256,11 @@ main(int argc, char *argv[])
219256
if (ret < 0)
220257
exit(ret);
221258

222-
ret = krun_signal();
259+
sock_fd = krun_signal();
260+
if (sock_fd < 0)
261+
exit(ret);
262+
263+
ret = rootfs_rcv(sock_fd);
223264
if (ret < 0)
224265
exit(ret);
225266

src/nitro/src/enclaves.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,11 @@ pub struct NitroEnclave {
4040
impl NitroEnclave {
4141
/// Run the enclave.
4242
pub fn run(&mut self) -> Result<u32> {
43-
let _rootfs_archive = self.rootfs_archive()?;
43+
let rootfs_archive = self.rootfs_archive()?;
4444

45-
let (cid, _stream) = self.start()?;
45+
let (cid, stream) = self.start()?;
46+
47+
self.write_rootfs(rootfs_archive, stream)?;
4648

4749
Ok(cid)
4850
}
@@ -124,4 +126,21 @@ impl NitroEnclave {
124126

125127
Ok((cid, stream.0))
126128
}
129+
130+
fn write_rootfs(&self, archive: Vec<u8>, mut stream: VsockStream) -> Result<()> {
131+
let len: u64 = archive
132+
.len()
133+
.try_into()
134+
.or(Err(NitroError::RootFsTooLarge))?;
135+
136+
stream
137+
.write_all(&len.to_ne_bytes())
138+
.map_err(NitroError::RootFsLenWrite)?;
139+
140+
stream
141+
.write_all(&archive)
142+
.map_err(NitroError::RootFsWrite)?;
143+
144+
Ok(())
145+
}
127146
}

src/nitro/src/error.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ pub enum NitroError {
2323
VsockSetTimeout,
2424
VsockConnect,
2525
IpcWrite(io::Error),
26+
RootFsLenWrite(io::Error),
27+
RootFsWrite(io::Error),
28+
RootFsTooLarge,
2629
}
2730

2831
impl fmt::Display for NitroError {
@@ -64,6 +67,13 @@ impl fmt::Display for NitroError {
6467
NitroError::IpcWrite(e) => {
6568
format!("unable to write enclave vsock data to UNIX IPC socket: {e}")
6669
}
70+
NitroError::RootFsLenWrite(e) => {
71+
format!("unable to write rootfs archive length to enclave: {e}")
72+
}
73+
NitroError::RootFsWrite(e) => {
74+
format!("unable to write rootfs archive to enclave: {e}")
75+
}
76+
NitroError::RootFsTooLarge => "rootfs size is larger than 64 bytes".to_string(),
6777
};
6878

6979
write!(f, "{}", msg)

0 commit comments

Comments
 (0)