Skip to content

Commit 889de6a

Browse files
committed
nitro: Write rootfs tar archive to enclave
1 parent b23293a commit 889de6a

3 files changed

Lines changed: 53 additions & 9 deletions

File tree

init/nitro/main.c

Lines changed: 30 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,32 +178,50 @@ 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;
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;
197214
}
198215

216+
memcpy(&len, len_b, sizeof(uint64_t));
217+
199218
return 0;
200219
}
201220

202221
int
203222
main(int argc, char *argv[])
204223
{
205-
int ret;
224+
int ret, sock_fd;
206225

207226
// Block all signals.
208227
ret = sig_mask(SIG_BLOCK);
@@ -219,7 +238,11 @@ main(int argc, char *argv[])
219238
if (ret < 0)
220239
exit(ret);
221240

222-
ret = krun_signal();
241+
sock_fd = krun_signal();
242+
if (sock_fd < 0)
243+
exit(ret);
244+
245+
ret = rootfs_rcv(sock_fd);
223246
if (ret < 0)
224247
exit(ret);
225248

src/nitro/src/enclaves.rs

Lines changed: 17 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,17 @@ 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+
Ok(())
141+
}
127142
}

src/nitro/src/error.rs

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

2830
impl fmt::Display for NitroError {
@@ -64,6 +66,10 @@ impl fmt::Display for NitroError {
6466
NitroError::IpcWrite(e) => {
6567
format!("unable to write enclave vsock data to UNIX IPC socket: {e}")
6668
}
69+
NitroError::RootFsLenWrite(e) => {
70+
format!("unable to write rootfs archive length to enclave: {e}")
71+
}
72+
NitroError::RootFsTooLarge => "rootfs size is larger than 64 bytes".to_string(),
6773
};
6874

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

0 commit comments

Comments
 (0)