|
19 | 19 | #include <sys/wait.h> |
20 | 20 | #include <linux/vm_sockets.h> |
21 | 21 |
|
| 22 | +#include <nsm.h> |
| 23 | + |
22 | 24 | #include "include/archive.h" |
23 | 25 | #include "include/fs_init.h" |
24 | 26 | #include "include/cgroups_init.h" |
|
30 | 32 | #define VSOCK_CID 3 |
31 | 33 | #define VSOCK_PORT 9000 |
32 | 34 |
|
| 35 | +#define NSM_PCR_ROOTFS 16 |
| 36 | +#define NSM_PCR_EXEC_ENV 17 |
| 37 | + |
| 38 | +#define NSM_PCR_DATA_SIZE 256 |
| 39 | + |
33 | 40 | /* |
34 | 41 | * Block or unblock signals. |
35 | 42 | * |
@@ -285,6 +292,71 @@ launch(char **argv, char **envp) |
285 | 292 | return 0; |
286 | 293 | } |
287 | 294 |
|
| 295 | +static int |
| 296 | +nsm_pcr_extend(int nsm_fd, int pcr, void *data, uint32_t size) |
| 297 | +{ |
| 298 | + uint32_t pcr_data_size; |
| 299 | + uint8_t pcr_data[NSM_PCR_DATA_SIZE]; |
| 300 | + bool locked; |
| 301 | + int ret; |
| 302 | + |
| 303 | + pcr_data_size = NSM_PCR_DATA_SIZE; |
| 304 | + |
| 305 | + ret = nsm_describe_pcr(nsm_fd, pcr, &locked, pcr_data, &pcr_data_size); |
| 306 | + if (ret != ERROR_CODE_SUCCESS) |
| 307 | + return -ret; |
| 308 | + |
| 309 | + ret = nsm_extend_pcr(nsm_fd, pcr, (uint8_t *) data, size, pcr_data, |
| 310 | + &pcr_data_size); |
| 311 | + if (ret != ERROR_CODE_SUCCESS) |
| 312 | + return -ret; |
| 313 | + |
| 314 | + return 0; |
| 315 | +} |
| 316 | + |
| 317 | +static int |
| 318 | +nsm_pcr_extend_rootfs_exec(void *rootfs, uint32_t rootfs_size, char *exec_path, |
| 319 | + char **exec_argv, char **exec_envp) |
| 320 | +{ |
| 321 | + char *c; |
| 322 | + int ret, nsm_fd; |
| 323 | + |
| 324 | + nsm_fd = nsm_lib_init(); |
| 325 | + if (nsm_fd < 0) |
| 326 | + return -1; |
| 327 | + |
| 328 | + ret = nsm_pcr_extend(nsm_fd, NSM_PCR_ROOTFS, rootfs, rootfs_size); |
| 329 | + if (ret < 0) |
| 330 | + return ret; |
| 331 | + |
| 332 | + ret = nsm_lock_pcr(nsm_fd, NSM_PCR_ROOTFS); |
| 333 | + if (ret != ERROR_CODE_SUCCESS) |
| 334 | + return -ret; |
| 335 | + |
| 336 | + ret = nsm_pcr_extend(nsm_fd, NSM_PCR_EXEC_ENV, exec_path, |
| 337 | + strlen(exec_path)); |
| 338 | + if (ret < 0) |
| 339 | + return ret; |
| 340 | + |
| 341 | + for (int i = 0; (c = exec_argv[i]) != NULL; ++i) { |
| 342 | + ret = nsm_pcr_extend(nsm_fd, NSM_PCR_EXEC_ENV, c, strlen(c)); |
| 343 | + if (ret < 0) |
| 344 | + return ret; |
| 345 | + } |
| 346 | + |
| 347 | + for (int i = 0; (c = exec_envp[i]) != NULL; ++i) { |
| 348 | + ret = nsm_pcr_extend(nsm_fd, NSM_PCR_EXEC_ENV, c, strlen(c)); |
| 349 | + if (ret < 0) |
| 350 | + return ret; |
| 351 | + } |
| 352 | + |
| 353 | + ret = nsm_lock_pcr(nsm_fd, NSM_PCR_EXEC_ENV); |
| 354 | + if (ret != ERROR_CODE_SUCCESS) |
| 355 | + return -ret; |
| 356 | + |
| 357 | + return 0; |
| 358 | +} |
| 359 | + |
288 | 360 | int |
289 | 361 | main(int argc, char *argv[]) |
290 | 362 | { |
@@ -332,6 +404,11 @@ main(int argc, char *argv[]) |
332 | 404 | if (ret < 0) |
333 | 405 | exit(ret); |
334 | 406 |
|
| 407 | + ret = nsm_pcr_extend_rootfs_exec(rootfs_archive, archive_size, exec_path, |
| 408 | + exec_argv, exec_envp); |
| 409 | + if (ret < 0) |
| 410 | + exit(ret); |
| 411 | + |
335 | 412 | ret = rootfs_mount(); |
336 | 413 | if (ret < 0) |
337 | 414 | exit(ret); |
|
0 commit comments