File tree Expand file tree Collapse file tree 3 files changed +22
-1
lines changed Expand file tree Collapse file tree 3 files changed +22
-1
lines changed Original file line number Diff line number Diff line change 8
8
#include < string>
9
9
#include < string_view>
10
10
#include < type_traits>
11
+ #include < unistd.h>
11
12
#include < vector>
12
13
13
14
#define FMT_HEADER_ONLY
@@ -203,4 +204,18 @@ namespace ccf::nonstd
203
204
tuple_for_each<I + 1 >(t, f);
204
205
}
205
206
}
207
+
208
+ static void close_fd (int * fd)
209
+ {
210
+ if (fd != nullptr && *fd >= 0 )
211
+ {
212
+ close (*fd);
213
+ *fd = -1 ;
214
+ }
215
+ }
216
+ using CloseFdGuard = std::unique_ptr<int , decltype (&close_fd)>;
217
+ static inline CloseFdGuard make_close_fd_guard (int * fd)
218
+ {
219
+ return CloseFdGuard (fd, close_fd);
220
+ }
206
221
}
Original file line number Diff line number Diff line change 2
2
// Licensed under the Apache 2.0 License.
3
3
#pragma once
4
4
5
+ #include " ccf/ds/nonstd.h"
5
6
#include " ccf/pal/attestation_sev_snp.h"
6
7
7
8
#include < fcntl.h>
@@ -106,8 +107,10 @@ namespace ccf::pal::snp::ioctl5
106
107
int fd = open (DEVICE, O_RDWR | O_CLOEXEC);
107
108
if (fd < 0 )
108
109
{
109
- throw std::logic_error (fmt::format (" Failed to open \" {}\" " , DEVICE));
110
+ throw std::logic_error (
111
+ fmt::format (" Failed to open \" {}\" ({})" , DEVICE, fd));
110
112
}
113
+ auto close_guard = nonstd::make_close_fd_guard (&fd);
111
114
112
115
// Documented at
113
116
// https://www.kernel.org/doc/html/latest/virt/coco/sev-guest.html
Original file line number Diff line number Diff line change 2
2
// Licensed under the Apache 2.0 License.
3
3
#pragma once
4
4
5
+ #include " ccf/ds/nonstd.h"
5
6
#include " ccf/pal/attestation_sev_snp.h"
6
7
7
8
#include < algorithm>
@@ -229,6 +230,7 @@ namespace ccf::pal::snp::ioctl6
229
230
throw std::logic_error (
230
231
fmt::format (" Failed to open \" {}\" ({})" , DEVICE, fd));
231
232
}
233
+ auto close_guard = nonstd::make_close_fd_guard (&fd);
232
234
233
235
// Documented at
234
236
// https://www.kernel.org/doc/html/latest/virt/coco/sev-guest.html
@@ -283,6 +285,7 @@ namespace ccf::pal::snp::ioctl6
283
285
throw std::logic_error (
284
286
fmt::format (" Failed to open \" {}\" ({})" , DEVICE, fd));
285
287
}
288
+ auto close_guard = nonstd::make_close_fd_guard (&fd);
286
289
287
290
// This req by default mixes in HostData and the CPU VCEK
288
291
DerivedKeyReq req = {};
You can’t perform that action at this time.
0 commit comments